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.
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.
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):
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.
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.
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
SWIFT 4+: shorter version
UIFont.systemFont(ofSize: 14.0, weight: .regular)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With