قالب وردپرس درنا توس
Home / IOS Development / # 309: UIFontMetrics 📐 – Little Bites of Cocoa

# 309: UIFontMetrics 📐 – Little Bites of Cocoa



Subjects

Topic

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.

Enter UIFontMetrics ! We can use this new type in iOS 1

1 to ask the system to scale the font size for us:

  la    headlineMetrics    =    UIFontMetrics  (  forTextStyle :      title ) 
  la    fontBeforeScaling    =    UIFont  (  name .    "Chicago"  19659019]    size ~~ POS = HEADCOMP .    16.0 )    la    font    =    headlineMetrics   scaledFont  ( [19659017] for :    fontBeforeScaling ) 

Tidy!

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  ([19659011]                  ] 
  la    heightBeforeScaling    =    44.0 
  la    height    =    headlineMetrics [19659014]  scaledValue  (  forValue :    heightBeforeScaling ) 

No major text in small buttons. Very cool!


Source link