www.robkerr.com
www.robkerr.com

mobile development, cloud computing and building great software

Rob Kerr
Author

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Share


Tags


Twitter


Using the System Font Efficiently in iOS

Rob KerrRob Kerr

Often an app designer specifies custom fonts out of a perceived need for uniqueness, but very often the built-in iOS system font is entirely appropriate.

One advantage of the system-provided font is how simple it is to load them from code. Here's how to do it:

Call systemFontOfSize

When using the system font, call the static routine systemFont to generate a font with a weight and height.

UIFont.systemFont(ofSize: <fontSize>, 
              weight: <predefined weight value>)

Specify a font size for the height of the font

The fontSize is per usual -- for example 10.0 for a standard reading font.

Specify a font weight using predefined constants

For predefined weight value, pass in one of the following constants, which will let iOS go find the correct font for you from the pre-installed System font used on the device.

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy

For Example

Example valid calls:

let fontNormal = UIFont.systemFont(ofSize: 10.0, 
              weight: UIFontWeightLight)
let fontHeading = UIFont.systemFont(ofSize: 12.0, 
              weight: UIFontWeightBold)

Caveats

There are a few caveats to be aware of:

Rob Kerr
Author

Rob Kerr

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Comments