Set Specific Font Weight for UILabel in Swift

Dave G picture Dave G · Feb 19, 2018 · Viewed 12.1k times · Source

When people have asked how to set a bold font, most people suggest:

let boldFont = UIFont.boldSystemFont(ofSize: ___)

But take a look at all the font weights that the standard system font offers:

Xcode system font weights

So my question is how do you set a light, semibold, or heavy font weight? The only way that I know how is:

sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];

However, I'm still asking because this isn't strongly typed. Most other class attributes are set by selecting from a fixed set of options and don't require passing a string that I could mistype. I guess I could set my own global enum... But any better ideas?

Answer

rmaddy picture rmaddy · Feb 19, 2018

I couldn't get the UIFontDescriptor to work with the font weight trait but there is another approach.

let font = UIFont.systemFont(ofSize: 20, weight: .light)

Replace .light with whichever value you want from UIFont.Weight which basically matches the dropdown list shown in your question.