Article

Getting a Scaled Font

Scale text in your interface automatically by creating fonts that support Dynamic Type.

Overview

Dynamic Type allows the user to choose the size of textual content displayed on the screen so they don't have to squint or strain their eyes when reading. Apps that support Dynamic Type also provide a more consistent reading experience.

To add support for Dynamic Type in your app, you need to use scaled fonts. A scaled font can be created from the system font or a custom font.

How to Use a Scaled System Font

Using the scaled system font is recommended when supporting Dynamic Type because it’s designed to look good at any size (for more information, see Typography). Plus, by using the system font you provide consistency with the system and other apps.

You should also use text styles when adding Dynamic Type support to your app. A text style describes the intention of the text, such as headline or body or title1, and lets the system know how best to adjust the size of the text.

If you’re using Interface Builder, select the text style from the font drop down, then mark the Dynamic Type option Automatically adjust font.

A partial screenshot of Interface Builder showing the text style "Body" as the selected font in the Attribute Inspector for the selected label.

If you want to do the same in your source code, use preferredFont(forTextStyle:). This method returns an UIFont that you can assign to a label, text field, or text view. Next, set the adjustsFontForContentSizeCategory property on the text control to true. This tells the text control that it should adjust the text size based on the Dynamic Type setting provided by the user.

label.font = UIFont.preferredFont(forTextStyle: .body)
label.adjustsFontForContentSizeCategory = true

How to Use a Scaled Custom Font

Dynamic Type also works with custom fonts—that is, any font that isn’t the system font. If you use a custom font in your app and want to let the user control the text size, you must create a scaled instance of the font in your source code.

To create a scaled custom font, call scaledFont(for:), passing in a reference to the custom font. You can use this call on the default font metrics or you can specify a text style; for instance, label.font = UIFontMetrics.headline.scaledFont(for:customFont). You must also set the adjustsFontForContentSizeCategory property to true on the text control in order to see the effects of Dynamic Type.

guard let customFont = UIFont(name: "CustomFont-Light", size: UIFont.labelFontSize) else {
    fatalError("""
        Failed to load the "CustomFont-Light" font.
        Make sure the font file is included in the project and the font name is spelled correctly.
        """
    )
}
label.font = UIFontMetrics.default.scaledFont(for: customFont)
label.adjustsFontForContentSizeCategory = true

See Also

Fonts

Adding a Custom Font to Your App

Add a custom font to your app and use it in your app’s interface.

class UIFont

The interface for getting and setting font information.

class UIFontDescriptor

A mechanism to describe a font with a dictionary of attributes.

struct UIFontDescriptor.SymbolicTraits

A symbolic description of the stylistic aspects of a font.

protocol UIContentSizeCategoryAdjusting

A collection of methods that give controls an easy way to adopt automatic adjustment to content category changes.

class UIFontMetrics

A utility object for obtaining custom fonts that scale to support Dynamic Type.