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.


Follow

Get every new post delivered to your Inbox.