How do I support Dynamic Type in UILabel
and UITextView
in iOS 7? I'm adapting one of our projects for iOS 7 and would like to support this accessibility feature. I can't find the specific how to tutorial on it on Apple's iOS Developer site.
Dynamic Type is a feature that enables the app's content to scale based on the user's preferred content size. The preferred content size can be found in Settings -> Accessibility -> Display & Text Size -> Larger Text.
Dynamic Type is a feature that was introduced in iOS 7 allowing users to change the default font size used across iOS. It is intended predominantly to support visually impaired users but in practice there are many iOS users who simply prefer a smaller / larger reading size for a variety of reasons.
To add support for Dynamic Type in your app, you use text styles. A text style describes the use of the text, such as headline or body or title1 , and lets the system know how best to adjust its size. You can configure text styles in either Interface Builder or your source code.
Fortunately, Safari's UIWebView (what Safari uses to display Web pages) handles this communication for you, but you have to do some work up front in your Web page. iOS has a feature called Dynamic Type (introduced in iOS 7) which communicates to applications what the user's preferred text size is.
If you use the new UIFont
methods then you're pretty much there - you just need to add the observer to listen for changes.
Rather than setting a specific font size, you should use the preferredFontForTextStyle:
and related methods when styling your labels (if you're using Interface Builder you can select a style directly in the inspector). For example:
self.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
Once you've done that you should listen for the UIContentSizeCategoryDidChangeNotification
. When you receive this notification you should layout your labels to support the newly selected size (if you're using autolayout this is normally as simple as sending invalidateIntrinsicContentSize
to your views).
If you're looking for official documentation then take a look at the Text Programming Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With