Change the Weight of the Font in Swift

Asked 2 years ago, Updated 2 years ago, 71 views

In Swift 2.0, how do I change the weight of the font with the code?It's easy to change on the Storyboard, but for various reasons I had to do it with the code.There is no problem functionally without changing the weight, but a slightly thinner weight makes it look better.

swift font

2022-09-30 14:26

1 Answers

The weight of the font can be specified by code only in system fonts.Except for the system font, the PostScript name will be directly designated as the UIFont initiator.
You can specify the weight of the system font by UIFont class method

class func systemFontOfSize(_fontSize:CGFloat, weight:CGFloat) ->UIFont

The second argument in , of type CGFloat, contains the following nine constants:

let UIFontWeightUltraLight—CGFloat
let UIFontWeightThin—CGFloat
let UIFontWeightLight—CGFloat
let UIFontWeightRegular:CGFloat
let UIFontWeightMedium—CGFloat
let UIFontWeightSemibold—CGFloat
let UIFontWeightBold—CGFloat
let UIFontWeightHeavy—CGFloat
let UIFontWeightBlack:CGFloat

and above, valid for iOS 8.2 and later.

You can specify nine weights, but only the system font San Francisco from iOS 9 is available.It doesn't work with Japanese font Hiragino Gothic, so you need to check it on the actual machine.

I think this site will be helpful.

Understand San Francisco system font from iOS 9


2022-09-30 14:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.