Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the iOS system font programmatically

I am trying to change the font size of the title of a navigation bar. I know I can set its attributes using:

var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]

...

 self.navigationController?.navigationBar.titleTextAttributes = attributes

What I cannot seem to find is the correct 'System' font name.

I was after the default, a.k.a System, font name. I tried printing all the available fonts only to discover it does not belong to a family and does not seem to have an explicit name.

like image 933
zevij Avatar asked Aug 22 '15 09:08

zevij


People also ask

What is the system font of iOS?

SF Pro. This neutral, flexible, sans-serif typeface is the system font for iOS, iPad OS, macOS and tvOS. SF Pro features nine weights, variable optical sizes for optimal legibility, four widths, and includes a rounded variant.

What is Xcode system font?

iOS 9 uses SanFranciscoUIDisplay and SanFranciscoText as their default font. SanFranciscoUIDisplay is used for titles and larger font wheres SanFranciscoText is used for paragraph text and smaller font. They are the System Font in Xcode or you can download them here (but you need a developer account):

What font does iOS 13 use?

In iOS 10, iOS 11, and iOS 12, Apple made additional tweaks to the system font, and in iOS 11, iOS 12, and iOS 13, it commonly is called "SF Pro" in Developer's Guidelines. Starting with iOS 13, it also became possible to install custom fonts.


3 Answers

I think you need:

NSFontAttributeName : UIFont.systemFontOfSize(19.0) 

Or the bold version:

NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0) 

See this guide for more info on user interface guidelines and fonts.

like image 170
Roland Keesom Avatar answered Sep 21 '22 13:09

Roland Keesom


You can access the system font like this, and even set the weight of the font:

  • Swift 3, Swift 4

    UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)

  • Swift 2

    UIFont.systemFontOfSize(18, weight: UIFontWeightLight)

For the font weight you have the choice between those constants, there available from iOS 8.2:

UIFontWeightUltraLight, UIFontWeightThin, UIFontWeightLight, UIFontWeightRegular, UIFontWeightMedium, UIFontWeightSemibold, UIFontWeightBold, UIFontWeightHeavy, UIFontWeightBlack 
like image 41
Philippe Avatar answered Sep 18 '22 13:09

Philippe


SWIFT 4+: shorter version

UIFont.systemFont(ofSize: 14.0, weight: .regular)
like image 29
Yaroslav Dukal Avatar answered Sep 19 '22 13:09

Yaroslav Dukal