Friday, April 14, 2017

Using Named iOS Font Styles With Custom Fonts

Curt Clifton:

Trying to change the family on the font descriptor gets us a descriptor that includes both the new family and the old named font style […] Creating a font from the confused descriptor takes us right back where we started […] Our Helvetica is nowhere to be found.

[…]

What if we had a way to maintain the style information even when switching to a custom font? With that available, we could look up the correct font size even when the user changes their preferred content size.

Here’s an extension on UIFont that does just that. The key trick is using associated objects to piggyback style information on the font.

[…]

In awakeFromNib the fonts will start as those set in the nib or storyboard but will end as our custom fonts. In traitCollectionDidChange the fonts will start as our custom ones and will end as our custom ones at the new preferred content size.

Comments RSS · Twitter

Leave a Comment