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:
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
Example valid calls:
let fontNormal = UIFont.systemFont(ofSize: 10.0, weight: UIFontWeightLight) let fontHeading = UIFont.systemFont(ofSize: 12.0, weight: UIFontWeightBold)
There are a few caveats to be aware of:
- The System font used on different versions of iOS is not necessarily the same. For example on iOS 9, the font is
San Francisco. For iOS 8 the font is
Helvetica Neue. This could be good or bad, depending on your point of view. It’s good that your app will automatically adopt whatever fresh font Apple introduces in the future (if it does). On the other hand, if you use System font, you don’t have control over the font rendered in your app.
- The systemFont (which used to be called systemFontOfSize before Swift 3.0, is available only on iOS 8.2 or later. This shortcut method won’t work if you’re supporting a codebase with an OS target before the minimum. Be warned.
Originally published at rekerrsive.ghost.io on December 1, 2016.