How to honor Dynamic Type Accessibility Sizes with a custom font in an iOS storyboard

timeSmith picture timeSmith · Feb 12, 2016 · Viewed 9.8k times · Source

How can I use the dynamic type text style "Title 1" and set the font face to the built-in font Chalkboard SE for a UILabel in a storyboard?

I need to honor the Dynamic Type size in iOS (Apple has been encouraging this since iOS 7?) I also need to use the built-in font Chalkboard SE, which is not used by default for the "text styles" fonts. I am currently using a custom font as shown in the image, but need the font to change size in accordance with the user's Dynamic Type/Accessibility Sizes preference just as all the Text Styles fonts do. The best Text Styles option is Title 1, but the font/typeface is unacceptable.

Font menu in Xcode. Custom checked and Title 1 highlighted

Answer

user4151918 picture user4151918 · Feb 12, 2016

Although you can't specify both a custom font and a preferred text style via Storyboard, it's not difficult to programmatically specify a dynamic type size for your custom font:

Swift:

let pointSize  = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFont‌​TextStyleTitle1).poi‌​ntSize
let customFont = UIFont(name: "Chalkboard SE", size: pointSize)

When you receive a UIContentSizeCategoryDidChangeNotification, use the same code to update your label's font.

Obj C:

 CGFloat pointSize = [[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline] pointSize];
 [titleLabel setFont:[UIFont fontWithName:@"Marker Felt" size:pointSize]];