Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIFont - how to get system thin font

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.

like image 321
MiguelSlv Avatar asked Aug 02 '15 11:08

MiguelSlv


People also ask

What is the name of iOS system font?

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. SF Pro supports over 150 languages across Latin, Greek, and Cyrillic scripts.


2 Answers

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.

like image 72
s1ddok Avatar answered Sep 29 '22 04:09

s1ddok


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.

like image 31
Fahim Parkar Avatar answered Sep 29 '22 03:09

Fahim Parkar