I needed to add a custom font to a project today and had real trouble finding how to do it. There were lots and lots of iOS tutorials, with this being the best of them. But I was doing a Mac OS app, not an iOS app, and so the tutorial didn’t quite fit my needs.
In the end, a coworker helped me figure out what I was doing wrong. So here’s a quick set of steps, based on the Code with Chris link above.
- Include the fonts in your Xcode project.
- Make sure they’re included in the target you’re building.
- Double-check that they’re being copied as resources into the product bundle.
- Include the folder with your fonts in the app Plist. On Mac OS you need to use
ATSApplicationFontsPath
instead ofUIAppFonts
. Important: Don’t think you can leave this out just because your fonts aren’t in a subfolder! Even if they’re loose in Resources, you need to supply a path. Just use “.” in that case. - Find the name of the font. On OS X, you can use this line:
NSLog(@"%@",[[NSFontManager sharedFontManager] availableFontFamilies]);
- Use NSFont and NSAttributedString to create a string using the font:
NSFont *font = [NSFont fontWithName:@"MyFont" size:20.0]; NSDictionary *attributes = @{NSFontAttributeName : font}; NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"But a virgin Wurlitzer heart never once had a song" attributes:attributes];