Add Custom Font

iPhone SDK Tips & Tricks

  1. Add your font file (for example, Chalkduster.ttf) to Resources folder of the project in XCode.
  2. Open info.plist and add a new key called UIAppFonts. The type of this key should be array.
  3. Add your custom font name to this array including extension ("Chalkduster.ttf").
  4. Now you can use [UIFont fontWithName:@"Chalkduster" size:16] in your application.

Unfortunately, IB doesn't allow to initialize labels with custom fonts.

myLabel.font= [UIFont fontWithName:@"Chalkduster" size:18];


Sometimes font name is different from filename, you can check the correct font name with this:

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;

NSInteger indFamily, indFont;

for (indFamily=0; indFamily<[familyNames count]; ++indFamily)

{

NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);

fontNames = [[NSArray alloc] initWithArray:

[UIFont fontNamesForFamilyName:

[familyNames objectAtIndex:indFamily]]];

for (indFont=0; indFont<[fontNames count];

++indFont)

{

NSLog(@" Font name: %@", [fontNames objectAtIndex:indFont]);

}

[fontNames release];

}



For xcode 4.3 or later, make sure your fonts are included in your bundle:

  • Click your project
  • Go to build phases
  • Under 'Copy Bundle Resources', make sure your fonts are included

Change Color/Style of the iPhone Status Bar

iPhone SDK Tips & Tricks

add the following line into your applicationDidFinishLaunching method:

[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];

Adding days to NSDate

iPhone SDK Tips & Tricks

NSDate *now = [NSDate date];
int daysToAdd = 50; // or 60 :-)

// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];

// create a calendar
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
NSLog(@"Clean: %@", newDate2);

Round Number

iPhone SDK Tips & Tricks


#import "math.h"

float numberToRound;
int result;

numberToRound = 4.51;

result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(4.510000) = 5

result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(4.510000) = 5

result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(4.510000) = 4

Remove or Delete User Default Data

iPhone SDK Tips & Tricks

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"dataName"];

Get iPhone Device Name, Unique Device Identifier (UDID), OS and Model

iPhone SDK Tips & Tricks

NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
NSLog(@"name: %@", [[UIDevice currentDevice] name]);
NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"model: %@", [[UIDevice currentDevice] model]);
NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);

Check if string null or not

iPhone SDK Tips & Tricks

if([myStr isEqual: [NSNull null]])
NSLog(@"null");
else
NSLog(@"not null");

Crop Image

iPhone SDK Tips & Tricks

UIImageView *tempImageview;
CGRect newRect = CGRectMake(0, 0, newWidth, tempImageview.frame.size.height);
CGImageRef tmp = CGImageCreateWithImageInRect([tempImageview.image CGImage], newRect);
newRect = CGRectMake(tempImageview.frame.origin.x, tempImageview.frame.origin.y, newWidth, tempImageview.frame.size.height);

tempImageview.frame = newRect;
tempImageview.image = [UIImage imageWithCGImage:tmp];

Bring Image or View to the Front

iPhone SDK Tips & Tricks

[self.view bringSubviewToFront:myImageView];

Other relevant function:

– bringSubviewToFront:
– sendSubviewToBack:
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– exchangeSubviewAtIndex:withSubviewAtIndex:

add Image Border

iPhone SDK Tips & Tricks

#import <QuartzCore/QuartzCore.h>

[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];