How to access the iOS system font programmatically

IosSwiftCocoa Touch

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Solution 3 - Ios

SWIFT 4+: shorter version

UIFont.systemFont(ofSize: 14.0, weight: .regular)

Solution 4 - Ios

(In line with the answer from Philippe for the latest version)

  • Swift 4

    UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)

Solution 5 - Ios

Besides all the answers, it's a better idea to use system font with system styles instead of defining custom sizes and weights. To access them programmatically, for example for the headline, you can use this method:

let font = UIFont.preferredFont(forTextStyle: .headline)

I know it is a valid code at least for Swift 5.

Solution 6 - Ios

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]

Solution 7 - Ios

Just use methods of UIFont (swift):

let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())

Hope it helps!

Solution 8 - Ios

Try the below code:

self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzevijView Question on Stackoverflow
Solution 1 - IosRoland KeesomView Answer on Stackoverflow
Solution 2 - IosPhilippeView Answer on Stackoverflow
Solution 3 - IosYaroslav DukalView Answer on Stackoverflow
Solution 4 - IosVincentView Answer on Stackoverflow
Solution 5 - IosJustinView Answer on Stackoverflow
Solution 6 - IosSujith ChandranView Answer on Stackoverflow
Solution 7 - Iosmiche.atuchaView Answer on Stackoverflow
Solution 8 - IosAditi AgrawalView Answer on Stackoverflow