How to use a custom font with dynamic text sizes in iOS7

Viewed 30428

In iOS7 there are new API's for getting a font that is automatically adjusted to the text size the user has set in their preferences.

It looks something like this to use it:

UIFont *myFont = [UIFont fontWithDescriptor:[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline] size:0];

Now whatever text you assign this to will move up and down in font size as the user changes their system text size setting. (Remember to listen to the name:UIContentSizeCategoryDidChangeNotification notification and update your view to account for the change in size).

How do I use dynamic text with a font other than the default Helvetica-Neue?

12 Answers

In iOS 11 you can use:

var customFont = UIFont.systemFont(ofSize: 17.0)
if #available(iOS 11.0, *) {
    customFont = UIFontMetrics.default.scaledFont(for: customFont)
}
// use customFont...

See also Building Apps with Dynamic Type WWDC 2017 - Session 245 - iOS time 8:34.

Starting from iOS 11 you can programmatically adjust custom fonts configured in visual editor:

  1. Configure your custom font using visual editor
  2. Adjust font size in viewDidLoad method:
@IBOutlet weak var leftLangLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    leftLangLabel.font = UIFontMetrics.default.scaledFont(for: leftLangLabel.font)
}
Related