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:
Currently, there are 7 different
UIContentSizeCategory sizes, ranging from XS-XXXL.
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
- Caption1, Caption2
This table shows the actual font size when you combine your text style and your user preferred text size:
Knowing what text style you want to use, you can then:
- Get the preferred UIFont
- Get the preferred UIFontDescriptor
We all (should) be familiar with
What is this
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.
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
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.didChangePreferredContentSize), name: UIContentSizeCategoryDidChangeNotification, object: nil)
Then you need to update your views in your method
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.