Skip to content

Instantly share code, notes, and snippets.

@nvkiet
Created July 6, 2014 16:56
Show Gist options
  • Save nvkiet/91ef9f0bda5a80ead551 to your computer and use it in GitHub Desktop.
Save nvkiet/91ef9f0bda5a80ead551 to your computer and use it in GitHub Desktop.
How to use custom font in iOS
You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.
Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:
UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment