Categories
iOS Development

How to use new iOS 13 system fonts like New York

At the WWDC19 there was introduced a lot of font related cool stuff for iOS13. One great thing is the new serif system font NewYork which can be used in your apps.

To initialize a UIFont with this or any other of the new font styles, we have to use UIFontDescriptors. I did not know about these before but they basically allow you to initialise a font by describing its characteristics, e.g. the style of the font or its family name.

So here we go:

/// We use a computed property so that we can use this font easily wherever we want to.
var newYorkFont: UIFont {
/// 1. Initialize a system font with the preferred size and weight and access its `fontDescriptor` property.
      let descriptor = UIFont.systemFont(ofSize: 24,
                                         weight: .semibold).fontDescriptor

/// 2. Use the new iOS13 `withDesign` to get the `UIFontDescriptor` for a serif version of your system font. The size is derived from your initial `UIFont` so set it to `0.0`
      if let serif = descriptor.withDesign(.serif) {
        return UIFont(descriptor: serif, size: 0.0)
      }

/// 3. Initialize a font with the serif descriptor of your system font. Again: use `0.0` as `size` parameter to prevent overriding the initial size we did set above.
      return UIFont(descriptor: descriptor, size: 0.0)
    }Code language: JavaScript (javascript)

It’s always nice to use system fonts because they are already installed on your iOS device which means you do not have to load them manually into your bundle. With iOS 13 there were a lot of great looking fonts introduced (see: https://developer.apple.com/fonts/system-fonts/ and link below).


Sources:

Leave a Reply

Your email address will not be published. Required fields are marked *