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
Helveticais 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
UIFontthat does just that. The key trick is using associated objects to piggyback style information on the font.
awakeFromNibthe fonts will start as those set in the nib or storyboard but will end as our custom fonts. In
traitCollectionDidChangethe fonts will start as our custom ones and will end as our custom ones at the new preferred content size.
Stay up-to-date by subscribing to the Comments RSS Feed for this post.