UIFont - how to get system thin font

IosObjective CSwiftUikitUifont

Ios Problem Overview


UIFont has methods to get regular font (systemFontOfSize) or bold font (boldSystemFontOfSize), but how to get a "thin system font" available through storyboard?

Passing "system-thin" to UIFont Contructor doesn't work, this constructor only works for non system fonts.

Ios Solutions


Solution 1 - Ios

You can use system font thin weight:

UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)

List of available weights for San Francisco:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack

san francisco font weights

As of iOS 11, UIFontWeight* was renamed to UIFont.Weight.*. More you can get here https://developer.apple.com/documentation/uikit/uifont.weight.

Solution 2 - Ios

As of iOS 8.2, you can now use UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat):

UIFont.systemFontOfSize(19, weight: UIFontWeightLight)

iOS SDK provided constants for weights:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy

Using system font is better than creating a font based on font name when you want to use system fonts since iOS can change their system fonts on iOS (like when they did with Helvetica Neue in iOS 7, and now, San Francisco in iOS 9).

So what I would suggest is to include TTF file of the font you want as use that ttf file as custom font and use the custom font in your app.

This is the special reason why I don't like Apple. Never go what Apple say. Always do what we want. Apple keep on changing Default font for every OS.

Solution 3 - Ios

Also if you want to keep same font size and just change weight then use from targeted element font size. For example:

demoLabel.font = UIFont.systemFont(ofSize: demoLabel.font.pointSize, weight: UIFontWeightThin)

with this you can keep default label font size and just change weight.

As of iOS 11, UIFontWeightThin was renamed to UIFont.Weight.thin. More you can get here https://developer.apple.com/documentation/uikit/uifont.weight.

Solution 4 - Ios

Swift 4.2 / Swift 5.0

 label.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.thin)

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
QuestionMiguelSlvView Question on Stackoverflow
Solution 1 - Ioss1ddokView Answer on Stackoverflow
Solution 2 - IosFahim ParkarView Answer on Stackoverflow
Solution 3 - IosGrandFelixView Answer on Stackoverflow
Solution 4 - IosPranitView Answer on Stackoverflow