Jun 22, 2010
Displaying Custom Fonts In Objective-C (iPhone SDK)
Coming from the Flash Platform I’m used to do a lot of font embedding in my projects. A few week ago I looked into this topic when programming an iPad application which needed a complete design makeover with custom fonts to spice it up a bit.
So how do you go about including custom fonts in an iPhone/iPad project? Well, it’s not that hard if you’re having a little bit of objective-c experience. You should download the FontLabel library, add it to your project and start creating FontLabel instances in code. I sense you feel the need for an example, so here it is:
FontLabel *questionLabel = [[FontLabel alloc] initWithFrame:CGRectMake(60, 115, 900, 130) fontName:@"DINPro-Medium" pointSize:30.0f]; questionLabel.textColor = [UIColor whiteColor]; questionLabel.textAlignment = UITextAlignmentCenter; questionLabel.lineBreakMode = UILineBreakModeTailTruncation; questionLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]; questionLabel.numberOfLines = 0; [self.view addSubview:questionLabel]; [questionLabel release];
“DINPro-Medium” is a TrueType font included in my resources bundle. Please do mind that FontLabel currently only accepts TrueType fonts, so you might want to convert some of those non-ttf fonts to ttf. A good online font converter can be found here: http://onlinefontconverter.com/
Or, you could just any font file into your project and in the AppName-Info.plist add property:
Fonts provided by application.
Then, each item of that array would be your full font name, e.g.
Item0 | trashhand.ttf
You can then use your font for a UILabel:
[label setFont: [UIFont fontWithName:@"trashhand" size:20]];
Great hint!