iOS 7
custom fonts
iOS development
typography
iOS design

Custom fonts in iOS 7

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

iOS 7 introduced a major visual redesign, and with it came improved support for custom typography. Before iOS 7, adding custom fonts to an app required workarounds and careful attention to font file management. Starting with iOS 7, the process became more streamlined, and developers gained access to Dynamic Type and a richer text rendering API.

This article walks through the complete process of adding custom fonts to an iOS project, from bundling the font files to using them in code, along with common issues you are likely to encounter.

Adding Font Files to Your Xcode Project

The first step is to include your .ttf or .otf font files in your Xcode project.

  1. Drag the font files into your Xcode project navigator.
  2. In the dialog that appears, make sure "Copy items if needed" is checked and that your app target is selected under "Add to targets."
  3. Verify the font files appear under your target's "Build Phases" tab, in the "Copy Bundle Resources" section. If they are missing from this list, the font will not be included in the app bundle and will fail to load at runtime.

Registering Fonts in Info.plist

iOS needs to know which custom fonts your app provides. You declare them in Info.plist using the UIAppFonts key (displayed as "Fonts provided by application" in the property list editor).

xml
1<key>UIAppFonts</key>
2<array>
3    <string>OpenSans-Regular.ttf</string>
4    <string>OpenSans-Bold.ttf</string>
5    <string>OpenSans-Italic.ttf</string>
6</array>

Each entry is the exact filename of the font as it exists in the bundle, including the file extension. Getting this wrong is the single most common reason custom fonts fail to load.

Finding the Correct Font Name

The font's filename and its internal PostScript name are often different. iOS uses the PostScript name when you create a UIFont object. To find the correct name, you can log all available fonts at launch.

objectivec
1// In application:didFinishLaunchingWithOptions:
2for (NSString *familyName in [UIFont familyNames]) {
3    NSLog(@"Family: %@", familyName);
4    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
5        NSLog(@"  Font: %@", fontName);
6    }
7}

Look for your custom font family in the output. The string printed under "Font:" is the name you pass to fontWithName:size:.

Using Custom Fonts in Code

Once the font is registered and you know its PostScript name, using it is straightforward.

Objective-C

objectivec
1UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
2titleLabel.font = [UIFont fontWithName:@"OpenSans-Bold" size:24.0];
3titleLabel.text = @"Welcome to the App";
4[self.view addSubview:titleLabel];

Swift

swift
1let titleLabel = UILabel(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
2titleLabel.font = UIFont(name: "OpenSans-Bold", size: 24.0)
3titleLabel.text = "Welcome to the App"
4view.addSubview(titleLabel)

If fontWithName:size: returns nil, the font was not loaded. Double-check the Info.plist entry, the bundle resources list, and the PostScript name.

Using Custom Fonts in Interface Builder

You can also set custom fonts directly in storyboards and XIB files. Select the label or text view, open the Attributes Inspector, click the font selector, and choose "Custom" from the font type dropdown. Your registered fonts will appear in the family list.

One caveat: Interface Builder sometimes reverts to the system font if it cannot find the custom font on the Mac running Xcode. Make sure the font is installed on your development machine or use the "Installed" fonts section rather than system fonts.

Dynamic Type Compatibility

iOS 7 introduced Dynamic Type, which lets users choose their preferred text size in Settings. To make custom fonts respect this preference, use UIFontMetrics (available in iOS 11 and later) to scale your custom font.

swift
1let baseFont = UIFont(name: "OpenSans-Regular", size: 17.0)!
2let scaledFont = UIFontMetrics.default.scaledFont(for: baseFont)
3
4label.font = scaledFont
5label.adjustsFontForContentSizeCategory = true

For iOS 7 through iOS 10, you would need to observe UIContentSizeCategoryDidChangeNotification and manually update fonts when the user changes their text size setting.

Common Pitfalls

  1. Wrong filename in Info.plist. The filename must match exactly, including capitalization and extension. OpenSans-Regular.TTF is not the same as OpenSans-Regular.ttf on a case-sensitive filesystem.
  2. Font not added to the correct target. If you have multiple targets (for example, a main app and a widget extension), each target needs its own Info.plist entry and its own copy of the font in Build Phases.
  3. Using the filename instead of the PostScript name in code. UIFont(name: "OpenSans-Regular.ttf", size: 16) will return nil. You need the PostScript name, which is typically something like OpenSans-Regular without the extension.
  4. Licensing issues. Many fonts are licensed for web use but not for app embedding. Verify that your font license covers mobile application distribution before shipping.
  5. Large font file sizes. Some font families include dozens of weights and styles. Each file adds to your app bundle size. Only include the weights you actually use.

Summary

Adding custom fonts in iOS 7 and later is a three-step process: include the font files in your bundle, register them in Info.plist, and reference them by their PostScript name in code. The most common failures come from mismatched filenames, missing bundle resources, or using the wrong name string. Log all available fonts during development to verify your fonts loaded correctly, and consider Dynamic Type compatibility so your app respects the user's accessibility preferences.


Course illustration
Course illustration

All Rights Reserved.