Change the Weight of the Font in Swift

Asked 1 months ago, Updated 1 months ago, 8 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


© 2022 OneMinuteCode. All rights reserved.