Extending UITextField to Have Validation

February 28, 2014

I’ve created a subclass of UITextField that adds validation facilities and appearance feedback.

Image

The idea is that users are happier when it is clear what is expected from them. If you have fields in a form, say, and some either NEED to be filled out or they NEED to be filled out in a specific way, the validating text field will indicate visually if the field has everything it needs.

Programmatically, it’s easy to set up. You either assign a block to the text field that returns a BOOL or implement the validation delegate protocol. The textfield will take care of its own appearance.

Get the code from here and I hope you find it useful!

https://github.com/jmenter/JAMValidatingTextField

Get a sample Xcode 5 project from here: https://dl.dropboxusercontent.com/u/3903183/TextFields.zip

Screen Shot 2014-03-01 at 1.51.39 PM


Simple SVG Parser for iOS

September 29, 2013

I’ve added a simple SVG Parser for iOS on github. Get it here: https://github.com/jmenter/JMSVGParser

Here’s how it works:

1.) Call the class method to get back an array of styled shapes

self.shapes = [JMSVGParser parseFileNamed:@"awesome_tiger"]

2.) In your view’s – (void)drawRect:(CGRect)rect method, tell each shape to draw, like this:

for (JMStyledPath *styledPath in self.shapes) {
    [styledPath drawStyledPath];
}

Screen Shot 2013-09-29 at 11.00.06 PM

That’s it! Comments, questions? Leave a note!


NSString Concatenation Using Categories and NSArray Literals.

August 23, 2013

A while back I had posted on concatenating NSString objects. Now that we have literal notation, let’s see how much easier and terse we can make this, hmmmm?

Here is the category on NSArray:

@interface NSArray (StringUtilities)
- (NSString *)string;
@end
@implementation NSArray (StringUtilities)
- (NSString *)string;
{
    return [self componentsJoinedByString:@""];
}
@end

Before, using my category on NSString would make concatenation work like this:

NSString *test = [NSString concatenate:@"This", @" is", @" a", @" test.", nil];

Now, the same thing with the category on NSArray looks like this:

NSString *test = @[ @"This", @" is", @" a", @" test." ].string;

Not as wordy and easier to type. I think I like it! Here’s a gist!


WWDC 2013: I’ll be there!

April 26, 2013

Thanks to my awesome employer and a bit of luck, I’ll be heading to WWDC 2013 in June! I was one of those people who was there at 10:00AM PDT but never made it through the ticket purchase process. Later that day, I got a call from Apple that said I would be getting an email with purchase instructions. I got the email, purchased the ticket, and it’s totally activated!

Hanging out with fellow Apple nerds at WWDC has been a dream of mine for over a decade. I’m super excited to learn and hang out with fellow developers.

Do you have any tips or suggestions on how I can make the most of it? Are you going and want to have a beer or two? Drop a comment or message!


Dot Notation in Objective-C

February 4, 2013

I’m going to jump right into this contentious issue. There has been a lot written about using dot notation in Objective-C. Before we get into the arguments, let me first show you something from some actual code I’m working on right now:

“Normal” bracket notation Objective-C:

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

Dot notation Objective-C 2.0:

fetchRequest.sortDescriptors = @[sort];

Alright, so I used some literals in there as well. But that’s really part of my point. The language evolves over time and just because something has “always been done this way” doesn’t mean it’s necessarily the best way to do it.

The dot notation line is almost half as long and, to my mind, much easier to read. Having been typing Objective-C for a few years now I can tell you that brackets are tough to manage. Xcode seems to get its “bracket helping” wrong almost half the time. And they are just visually cluttery.

Some say “OK, use dots but just for properties!” Me, I’m getting used to using the dots for everything except (void) methods with no arguments.

Everything else is up for grabs. I guess because I think of everything as a property and/or object to grab. For instance if I use:

UIColor.blackColor

I immediately think “OK, we’re peeling off a blackColor from the UIColor class.”

Here’s a more involved example. I have a “SensorManager” class that is a singleton. This class basically wraps up the functionality and behavior of the location manager and the motion manager. Here’s some actual code from the app I’m working on:

SensorSample *newSample = SensorSample.new;
sensorSample.latitude = @(SensorManager.sharedInstance.locationManager.location.coordinate.latitude);

This may make you cringe or it may make you go “ah-ha!”

When I look at it, I think to myself, “OK, we’ve got a new sample that we’re peeling off of the SensorSample class. We’re setting it’s latitude to a boxed up NSNumber that we get from the Sensor Manager’s shared instance’s location manager’s location’s latitude coordinate.

The other way to write this is:

SensorSample *newSample = [[SensorSample alloc] init];
[sensorSample setLatitude:[NSNumber numberWithFloat:[[[[SensorManager sharedInstance] locationManager] location] coordinate].longitude]];

ARE YOU FUCKING KIDDING ME?!?!? I couldn’t even type that second line without Xcode fucking up the brackets. Only it works fine IF YOU KNOW EXACTLY HOW MANY BRACKETS YOU HAVE TO TYPE FROM THE BEGINNING.

Also, the only time I needed to use the dot was to get the longitude out of the struct. Are the dot-notation-naysayers really saying that I have to type all those brackets because of that single dot attached to the struct? Whatever.

Geeze. This post went from casual to rage in a hurry.


A better UIPickerView

February 3, 2013

Working on a new/old project. Thought I would wrap my UIPickerView customizations into a self-contained class. Here it is.

I’ve improved UIPickerView by adding in presentation and interaction functionality that it should frankly have had from the beginning. Here’s what it does:

1.) Added a (void)show and (void)hide method. This makes the pickerView pop up from the bottom of the screen and pop back when asked. When the pickerView pops up, the background of the view controller (or navigation controller if that’s what we’re presenting in) dims and becomes a dismissal tap target.

2.) Added three delegate methods to notify clients if the pickerView has popped up, if it has disappeared, or if the selection indicator was tapped.

To use it in your view controller, just make your VC conform to the JMPickerViewDelegate protocol, and init it with a delegate and view controller. Easy peasy!

Get the code on GitHub or download the Xcode Project here.


NSString Concatenation (Category)

June 6, 2012

Hi folks. Just got back from a trip but I thought I would write you and tell you about this thing.

Sometimes, you just want to concatenate a bunch of strings together. There are several ways to do this. You can use stringByAppendingFormat with NSStrings or you can use append with NSMutableStrings. You can also add a bunch of strings to an array and generate a new string based on the array. Some languages overload the “+” operator to allow strings to be added together. Since Objective-C is a strict superset of C we can’t overload that operator.

So, what I’ve done is simply create a category on the NSString class. It contains a single class method called “concatenateStrings” that takes any number of arguments. Any number of NSString or NSMutableString arguments are concatenated together and returned as an NSString.

Get the example project from here.


JSON + iOS 5; GO! (Tutorial)

May 30, 2012

What is JSON? JSON stands for JavaScript Object Notation. It’s a super lightweight and human readable data format. Go check it out and run back here.

iOS 5 includes classes to deal with JSON natively. Which is awesome. Let’s do this!

Twitter has a service that gives you recent tweets in JSON format when you give it a URL. Like this:

https://api.twitter.com/1/statuses/user_timeline.json?screen_name=jmenter&count=10

That’ll give you my 10 most recent tweets in JSON format. Easy peasy. Let’s make an app.

In Xcode, New Project, Single View Application. Use ARC. Coolio.

Add a UITableView to the view. Add it as a property to the ViewController, add the data source and delegate and wire those up.

Add some methods to the ViewController.m: one to issue a request and one to handle it. Fix up the data source and delegate methods.

Done. This was not really a tutorial. Sorry to trick you.

Get the Xcode project here, check out the ViewController.m, and see how easy it is!


Showing and Dismissing a UIPickerView Control

April 28, 2012

There seems to be no standard way of dealing with UIPickerViews. Sometimes they are attached right to the bottom of the interface and never move around (like in the built-in Clock application.) Other times they fly up from the bottom like the keyboard but are embedded in a UIToolbar or UIActionSheet.

I’ve come up with a way of dealing with UIPickerView controls that I kind of like. Here’s the gist:

  1. The UIPickerView hides and shows very similar to how the keyboard hides and shows.
  2. When the UIPickerView shows, the rest of the view (including the navigation bar) is dimmed.
  3. Tap anywhere in the dimmed area to hide the UIPickerView. The dismissal tap just dismisses the UIPickerView and won’t be picked up by the view in the background.

To do this we create a custom UIView whose only job is to dim the background and receive the dismissal tap. Once everything is wired up, we hide and show the picker with – (void)showPickerView and – (void)dismissPickerView. Easy!

Here’s why I like it:

  1. Dimming the background puts the UI focus on picking. You can still see the background and the context in which the pickerView showed up (and it can still change state depending on what has been picked in the view.)
  2. A “done” button, while unambiguous, would cover up precious screen real estate and is a smaller target to hit than a dimmed background.
  3. At least for me, tapping the dimmed background to dismiss feels very natural. The fact that the background is dimmed implies that my taps will not affect any of the controls and whatnot in the background.

I’ve created an Xcode project that has an example. Feel free to take a look and use this in your own projects. The comments should make everything clear. I tried to think of a way to make the whole thing more modular (perhaps implementing this all as a subclass of UIPickerView?) but the complexities of having views in the navigation controller and animating everything confused me. Maybe when I’m smarter…

If you find this useful, let me know.


App-A-Week Challenge Update, also Font Tip! (Also, Snippets!)

April 16, 2012

PizzaPal has been updated to 1.1 with the following changes:

  • Removed iAds.
  • Added 4th pizza comparator.

Yeah, I know, that’s kind of cheap to count that as an app release. Whatever, making an app a week is no picnic.

Oh, also, if you do any kind of coding/programming, you should totally check this out:

I’ve switched my programming font of choice from the venerable Consolas for BBedit (14 pt) to the wonderful Inconsolata (15 pt). Inconsolata is simply a beautiful monospaced font that is easy to read, renders beautifully, and yet has TONS of character. My only criticism so far is that the underscore (_) character renders a bit thin. OH WELL. Seriously, download this font and give it a whirl. You shan’t be disappointed.

Also: holy crap why have I never used snippets in Xcode 4 before? Maybe because it’s not entirely obvious how to use them or maybe I was just not as smart a programmer as before. Anyway.

Here’s how to use snippets:

1) type out the thing you want to make a snippet of. Any “bubble text tab” thingees you can create with <#sometext#>. Drag it to the snippet library.

2) Give it a nice title, completion shortcut, and double-check for perfection:

3) Since snippets are sensitive to scope, you can make one for interface and one for implementation!

4) To use, just tap the completion shortcut letters, for example:

Basically, if you find yourself typing similar things over and over again in Xcode, make a snippet RIGHT NOW!


Follow

Get every new post delivered to your Inbox.