Really easy solution, that a lot of people tend to miss:

UILabel *randomLabel = [[UILabel alloc] init];
randomLabel.numberOfLines = 0;

Setting the numberOfLines property of UILabel will cause text to wrap automatically.
If you want it to wrap to a certain amount of lines… Say wrap to 3 lines then concat then it’s

randomLabel.numberOfLines = 3;

Honestly, this was much easier than I had expected.
A simple 3 lines of code.

QuartzCore must be imported to use the layer property, so load that up:

#import <QuartzCore/QuartzCore.h>

And now, to add a border:

UIView *randomView = [[UIView alloc] init];
randomView.layer.borderColor = [[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f] CGColor];
randomView.layer.borderWidth = 1.0f;

A common mistake is forgetting to use the CGColor property of the UIColor you select, so keep an eye out.
That code will give your view a 1px light grey border.

This is for those of you that may not want to create a subclass for the sole reason of catching taps on a UIView.

I stumbled across this issue in building my own, custom UITableView that’s a lot more customisable than the default.
Detecting taps through touchesBegan and touchesEnded also can get a bit messy, so I’ve found the best solution to this issue is UIGestureRecognisers.
There are several types for different types of gestures, but this post is about taps.

So to create a UIGestureRecognizer, you need a target and a method for it too call when it gets a tap.
the numberOfTapsRequired property defaults to 1, but I put it in there just for clarity. Change it to 2 if you want it to register double taps.

UITapGestureRecognizer *gestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
gestureRecogniser.numberOfTapsRequired = 1;
[self addGestureRecognizer:gestureRecogniser];
[gestureRecogniser release];

Implementation of the handleTap method:

- (void)handleTap:(UIGestureRecognizer*)tap {
    NSLog(@"I've been tapped!.");
}

Extremely quick post regarding when to use each type of property.
There are obviously exceptions, but for the majority of cases, this is what you’ll want to use.

assign

Should only be used with primitive objects (int, bool, etc).
This is because if an object you’ve set to a property using assign gets released by whatever owned it when you assigned it, then it will become a victim of premature deallocation, and you’ll get an exception or likely a bad access error if you attempt to reference that object again.

retain

Should be used with most Obj-C Objects, as this gives your class ownership of the object thus allowing you to be safe from premature deallocation. However, changes made to the object that was retained will still mirror onto your property.

copy

Should be used with NSString.
This is because strings are often mutated, and if you retain an NSMutableString as a NSString and the string gets mutated after the retain, then your property could be changing without you having knowledge of it.
Using copy will ensure that any changes made to the original object after assignment will have no effect on the property.

I was making an app recently and in order to line up perfectly with designs, I needed the placeholder text in my UITextFields to be a certain colour, size & font.
After quite a bit of googling I couldn’t find my answer, so I made my own. Subclassing seemed a bit overkill, so I decided to create a category to override theĀ drawPlaceholderInRect method. See the code below:

UITextField+Placeholder.h

#import <UIKit/UIKit.h>
@interface UITextField (Placeholder)
- (void) drawPlaceholderInRect:(CGRect)rect;
@end

UITextField+Placeholder.m

#import <UITextField+Placeholder.h>
@implementation UITextField (Placeholder)
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor colorWithRed:142.0f/255.0f green:205.0f/255.0f blue:236.0f/255.0f alpha:1.0f] setFill];
    [[self placeholder] drawInRect:rect withFont:[self font]];
}
@end

Then, in the files you wish to create UITextFields that have the custom Placeholders, simply import the header for your new category.

If you need to have several different UITextFields each with different placeholder colours then you’ll need to subclass.
The @interface and @implementation contents would be identical to if you used a Category.

In the latest iPhone app I created, I stumbled across a need to create a custom UINavigationBar from an Image.

Before iOS 5, there was no documentation on how to do this exactly, but it’s actually very easy.

Make sure your image is the correct size, and simply use this:

if([navController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-bar-bg-image.png"] forBarMetrics: UIBarMetricsDefault];
} else {
    [navController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav-bar-bg-image.png"]] autorelease] atIndex:0];
}
© 2012 The Life of a Dev Suffusion theme by Sayontan Sinha