简体   繁体   中英

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.

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

旧金山字体粗细

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

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.

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 .

斯威夫特4.2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM