How do I create a bold UIFont from a regular UIFont?

后端 未结 7 1903
醉话见心
醉话见心 2020-12-08 19:08

If I have a UIFont object, is it possible to convert it to bold? I don\'t know the font name, I just have a UIFont object. What I want is a function like

<
7条回答
  •  北海茫月
    2020-12-08 19:53

    This is a very old thread but someone may be interested in how to do this in Swift 5 nowadays.

    Easy like this:

    var font: UIFont = UIFont.systemFont(ofSize: 18)
    if let newDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
        font = UIFont(descriptor: newDescriptor, size: font.pointSize)
    }
    

提交回复
热议问题