Adding custom fonts to iOS app finding their real names
Use +[UIFont familyNames]
to list all of the font family names known to the system. For each family name, you can then use +[UIFont fontNamesForFamilyName:]
to list all of the font names known to the system. Try printing those out to see what name the system expects. Example code:
static void dumpAllFonts() {
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"%@", fontName);
}
}
}
Put that in your app, call it, and see what you get. If you see a name in the output that looks appropriate for your font, use it. Otherwise, perhaps you haven't properly added the font to your app.
In Swift:
func dumpAllFonts() {
for familyName in UIFont.familyNames {
for fontName in UIFont.fontNames(forFamilyName: familyName) {
print(fontName)
}
}
}
While it appears that UIFont
's fontNamed:size:
method is fairly forgiving with the names you provide, the official fontName
has always been the font's internal PostScript name. A font's PostScript name cannot contain spaces and usually has them replaced by a hyphen. I don't recall offhand, but I believe they may also be limited to 31 characters.
Determining what name you should use in code is extremely simple. Double click on the font you want to use on your iOS device to open it in Font Book. If the font is not already installed, click the Install button in the sample window that appears. When it's installed, select the font in the Font list. In Font Book's menu, choose Preview > Show Font Info. That will show info about the font like shown in the image below:
As you can see, the PostScript name for Helvetica Neue LT Std 65 Medium
is HelveticaNeueLTStd-Md
. That's the name you should use in code.
In your Info.plist
file, under fonts for the application, you need to use the actual filename of the font itself, whatever that happens to be. In my case, it was HelveticaNeueLTStd-Md.otf
.