33

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
jaytrixz
  • 4,059
  • 7
  • 38
  • 57
  • Check this tutorial [How to Support Dynamic Type in iOS 7 Apps.](http://mythoughtsandexperiments.blogspot.in/2013/12/how-to-support-dynamic-type-in-ios-7.html) – Adarsh V C Dec 10 '13 at 13:43

2 Answers2

57

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.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • Is there not a way to access the style that was picked in Interface Builder? If not, that seems like a big oversight. Now I have to set it in 2 places, which means it will inevitably get out of sync. – MacsimusPrime Aug 18 '15 at 20:31
  • `preferredFontForTextStyle` returns a system font though, is there any way to do this with custom fonts? – kevin Jan 09 '18 at 10:53
  • @kevin: `UIFontMetrics` class can scale any custom font according to the current font metrics: [UIFontMetrics documentation](https://developer.apple.com/documentation/uikit/uifontmetrics) – Vadim Belyaev Jul 29 '19 at 21:49
2

In Swift 3 and iOS 10 you can use

headline.font = UIFont.preferredFont(forTextStyle: UIFontTextStyleHeadline)
headline.adjustsFontForContentSizeCategory = true

See this excellent post for more information, especially how to support pre iOS 10.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393