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 *

Privacy Overview
mic.st

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.