Glad WWDC 2017 ! Today, we are starting to look at the incredibly large list of updates and improvements announced this week with
UIFontMetrics . Let's jump in.
We start with the problem we are trying to solve. It's about dynamic type.
Our users can adjust their preferred Dynamic Type value in Settings.app to display the text in our apps larger and more prominent.
This works well when we want to use the default font of the system since we can only type any code like this:
la font = UIFont . . ] : . title )
This will return a
UIFont suitable for the given text style adjusted by size and weight to match the user's Dynamic Type setting.
But what about custom fonts? In the past, this was a little cumbersome and we often had to resort to creepy hacking.
UIFontMetrics ! We can use this new type in iOS 1
la headlineMetrics = UIFontMetrics ( forTextStyle : title ) la fontBeforeScaling = UIFont ( name . "Chicago" 19659019] size ~~ POS = HEADCOMP . 16.0 ) la font = headlineMetrics scaledFont (  for : fontBeforeScaling )
There is always another fun trick tucked away in UIFontMetrics and it is scaling arbitrary values. This is great for helping us format our user interface elements (such as buttons or headers) to accommodate dynamic size fonts that live inside:
la headlineMetrics = UIFontMetrics ( ] la heightBeforeScaling = 44.0 la height = headlineMetrics  scaledValue ( forValue : heightBeforeScaling )
No major text in small buttons. Very cool!