This is a quick walk-through on using a font other than those supplied by Apple on an iOS device.
For reference purposes, you can find out what fonts are available to you “out of the box” by checking out this comprehensive list:
I’m going to be using Bebas for my example, a great font created by Dharma Type. You can pick it up here: http://www.dafont.com/bebas.font or use a font of your own choice. It’s important to note you should check a font’s license before you use it in an app you intend to distribute in the app store.
If your font’s not installed on your Mac, go ahead and install it. Before we get too deep into coding and while you’re in or around Font Book let’s go ahead and get the PostScript name of your font. You can do this by selecting your font from the list inside of Font Book and pressing Command + I to toggle the font information. The right side of the window will look like this:
The PostScript name is listed on the top, with Bebas, the PostScript name is simple… it’s Bebas but most are more complicated. Take the PTSans family for example: PTSans-Regular to PTSans-CaptionBold. Keep this PostScript name handy as we’ll reference it later.
Moving on let’s get the ttf file into an Xcode project.
I started with a Single View Application template, go ahead and get that going as normal. Inside my Supporting Files folder I’m going to create a group named “Fonts”. I’m going to drag
BEBAS___.TTF into that directory and make sure “Copy items into destination group’s folder (if needed)” is checked. Click finish.
Next, open your app’s plist. Right click and add a row, we’re going to add the key “Fonts provided by application” which is an array of the ttf font files. Toggle that down and for Item 0 add
Now you need to head over to your project’s build phases tab. Click to the “Copy Bundle Resources” and click the + icon to add a new item and choose
Now, when your window looks like this, you’re ready to use the font in the application:
I put some simple code to create a UILabel in my viewDidLoad method like this:
UILabel *bebasFlavoredLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 320, 44)]; bebasFlavoredLabel.text = @"Bebas on iPhone"; [bebasFlavoredLabel setFont: [UIFont fontWithName:@"Bebas" size:15]]; [self.view addSubview: bebasFlavoredLabel];
On line 3 you see where we use
[UIFont fontWithName:@"Bebas" size:15]. The name you use there is the PostScript name you found at the beginning. Go ahead and run:
Viola! Your font is ready to be used as you wish!