read

iOS 7 introduces Dynamic Type, a way for users to change their preferred text size from iPhone/iPad global Settings.

To know your user preferred settings:

UIApplication.sharedApplication().preferredContentSizeCategory

Currently, there are 7 different UIContentSizeCategory sizes, ranging from XS-XXXL.

Text Styles

Text Styles are semantic descriptions.

There are a fixed number of descriptions, and currently in iOS 9, 10 styles are supported:

  • Title1, Title2, Title3
  • Headline, Subheadline
  • Body
  • Footnote
  • Caption1, Caption2
  • Callout

This table shows the actual font size when you combine your text style and your user preferred text size:

Font Descriptor

Knowing what text style you want to use, you can then:

  1. Get the preferred UIFont preferredFontForTextStyle:
  2. Get the preferred UIFontDescriptor preferredFontDescriptorWithTextStyle:

We all (should) be familiar with UIFont.

What is this UIFontDescriptor?

UIFontDescriptor is introduced together with Dynamic Type in iOS 7, to provide a way to describe the attributes of a font, without actually creating a font object (which is more expensive).

1. System Font using UIFont

If you are okay with using the system font, then simply get the font with this 1 liner:

let titleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1)

To have the bold version, do this:

let boldDescriptor = titleFont.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
let boldFont = UIFont(descriptor: boldDescriptor!, size: 0)

2. Custom Font using UIFontDescriptor

If you want a custom font, then you have to use the intermediary.

// Get the preferred descriptor instead
let titleDescriptor = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleTitle1)

// Edit the trait and size, if needed
let boldTitleDescriptor = titleDescriptor.fontDescriptorWithSymbolicTraits(.TraitBold)

// Finally create the UIFont with the descriptor
let titleFont = UIFont(descriptor: boldTitleDescriptor, size: 0)

While you can edit the font to make it bold and change the font size etc.. there is a pitfall: You cannot change the font familiy or face.

To do that, you need to create UIFontDescriptor from scratch.

You will probably want to scale your custom font by a certain amount. Or use a helper method.

3. Storyboard

If you are using storyboard, you are limited to the system font.

Simply change Font > Text Style.

You will want to set the Minimum Font Scale (for UILabel). Do not use Minimum Font Size as that is deprecated.

As of Xcode 7.3, you cannot change the traits (eg. bold) from interface builder, but I belive the feature will come.

Detecting Settings Changes

When a user change his preferred text size in Settings, you need to listen to UIContentSizeCategoryDidChangeNotification.

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.didChangePreferredContentSize), name: UIContentSizeCategoryDidChangeNotification, object: nil)

Then you need to update your views in your method didChangePreferredContentSize.

You might want to use BNRDynamicTypeManager to “watch” over the views that need to be updated.

Testing on Simulator

Open Settings app, then go to General > Accessibility > Larger text.

However, note that simulator running on iOS 9.3 has a bug, so in some cases it might not work.

This is different from an actual device, which is under Display & Brightness > Text Size.


Image

@samwize

¯\_(ツ)_/¯

Back to Home