14

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Dave G
  • 12,042
  • 7
  • 57
  • 83
  • `class func systemFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont` – Leo Dabus Feb 19 '18 at 03:03
  • 1
    You can use the initializer above and choose any font weight. i.e: `let systemFont: UIFont = .systemFont(ofSize: 32, weight: .thin)` – Leo Dabus Feb 19 '18 at 03:03
  • Look into UIFontDescriptor. – rmaddy Feb 19 '18 at 03:09
  • @rmaddy there is only one bold option in font descriptor `UIFontDescriptorSymbolicTraits.traitBold` – Leo Dabus Feb 19 '18 at 03:15
  • 1
    Possible duplicate of [How do I create a bold UIFont from a regular UIFont?](https://stackoverflow.com/questions/16015916/how-do-i-create-a-bold-uifont-from-a-regular-uifont) – BB9z Nov 28 '18 at 06:05
  • Hi, I'm the author of the suggested duplicate question here. I don't think this question is a duplicate of the [other question](https://stackoverflow.com/q/16015916/754705). I posted my reasoning in the comments on the other question. – Michael Nov 28 '18 at 15:46

5 Answers5

31

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
18

You can use this extension. It assigns the weight to the fontDescriptor's weight key and instantiate your font with this new descriptor.

extension UIFont {
  func withWeight(_ weight: UIFont.Weight) -> UIFont {
    let newDescriptor = fontDescriptor.addingAttributes([.traits: [
      UIFontDescriptor.TraitKey.weight: weight]
    ])
    return UIFont(descriptor: newDescriptor, size: pointSize)
  }
}
arvinq
  • 656
  • 6
  • 12
  • This is not work for getting a regular weight font from a bold font, it still shows bold after adding regular weight trait. Test with New York font. – zgjie Nov 20 '21 at 09:58
  • Test with New York and San Francisco Rounded fonts. It seems this only works for San Francisco font. – zgjie Nov 20 '21 at 10:04
6

The very old thread, but someone may be interested in how to do it in Swift.

UIFont.Weight defines all of the options:

  • ultraLight
  • thin
  • light
  • regular
  • medium
  • semibold
  • bold
  • heavy
  • black

you can use simply like that, e.g.:

label.font = UIFont.systemFont(ofSize: size, weight: .bold)

or if you want to keep the previous size:

label.font = UIFont.systemFont(ofSize: label.font!.pointSize, weight: .bold)
Krzysztof Skrzynecki
  • 2,345
  • 27
  • 39
1

Even more:

let font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight(500))
Buhaescu Vlad
  • 78
  • 1
  • 6
0

If you want to use system fonts, for Swift 5 a simple extension would look like this:

extension UIFont {
    func withWeight(_ weight: UIFont.Weight) -> UIFont {
        UIFont.systemFont(ofSize: pointSize, weight: weight)
    }
}
CristianMoisei
  • 2,071
  • 2
  • 22
  • 28