Just incase you didn’t realise, with iOS 3.2 (iPad) and above you can load in custom fonts and use them with a standard UIFont object. There are a few catches… The font must be in the following format: –
Once you’ve dragged your chosen font file into an Xcode project, the next step is to add a line into the application’s Info.plist file. Add a new key UIAppFonts and make it an array. Expand the array and add a new string for each font, making the string the file’s full name including an extension.
You’re all set up now to use the font. That would be great if you knew which font it was! Here is a great little snippet for looping through all the fonts loaded into the system. Scan through the list and find your font.
Objective-C:
// Get all the fonts on the system
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames ){
printf( "Family: %s \n", [familyName UTF8String] );
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames ){
printf( "\tFont: %s \n", [fontName UTF8String] );
}
}
Swift:
// Get all the fonts on the system
UIFont.familyNames.forEach { familyName in
print("Family: \(familyName)")
UIFont.fontNames(forFamilyName: familyName).forEach { fontName in
print("\tFont: \(fontName)")
}
}
To use your font now, just use the standard UIFont constructor…
Objective-C:
self.titleLabel.font = [UIFont fontWithName:@"Inkpen Medium" size:31.0];
Swift:
self.titleLabel.font = UIFont(name: "Inkpen Medium", size: 31.0)
Some points to note: –
- You can also use the font inside UIWebViews.
- Interface Builder for XCode 3.2 has a bug that won’t let you choose the font. You have to do it in code.
- Loading in too many fonts will slow your loading time down and will hurt your users’ eyes.
I am finding an odd issue with this feature involving the order in which those strings are added to the UIAppFonts array. I am using two .otf files that represent two different font styles in the same family (Knockout-27.otf and Knockout-30.otf) as well as MyriadPro.otf. Using your code snippet I can see that all 3 of these custom fonts are available for the UIFont class. However, when trying to set these fonts, the two that are of the same family will not show up distinctly. What I mean is that I use them in two differently places in the App so I need both, but if I have Knockout-27.otf first in the array, then all labels that should have had fonts from the Knockout family use that style. If I put Knockout-30.otf in the file first, vice-versa. has anyone else experienced this problem? Where else can I look for a solution. It happens on device and in the simulator, in iOS4.1 and iOS4.2.
UPDATE: found a discussion on Apple’s dev forums that confirms that others have faced this issue. Seems to be a bug with no known workaround right now. I even tried converting my .otf files to .ttf and seeing if they were handled any differently, but that did not fix it either.
@Ankur, I had a similar problem actually and it seems to be a problem with the fonts. You can fix them using FontForge and changing the family name. See this post for details – https://devforums.apple.com/message/324012 Credit to Jeff Kelley for discovering a fix for this one.
Hey,
I want to use the capitals.dfont in xcode
capitals is a basic mac font, which can be found in fontlibrary app.
I am using xcode 4.3.3 and building for iphone.
I only work with storyboard so please help me with very easy instructions
tnx
when is add custom font file value in the info.plist and run the app in simulator;my simulator get hang in attaching to app name;after that app does not run over simulator. i am using xcode 4.3.1 ios 5.1
How to add custom font in xib. plz reply….
Awesome :) Really great and simple tutorial :)
Very helpful