Swift:以编程方式使UILabel加粗而不改变其大小?

时间:2023-01-16 16:43:20

I have a UILabel created programmatically. I would like to make the text of the label bold without specifying font size. So far I have only found:

我有一个以编程方式创建的UILabel。我想在不指定字体大小的情况下使标签文本变为粗体。到目前为止我只发现:

UIFont.boldSystemFont(ofSize: CGFloat) 

This is what I have exactly:

这就是我所拥有的:

let titleLabel = UILabel()
let fontSize: CGFloat = 26
titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabelFontSize)

But this way I am also setting the size. I would like to avoid that. Is there a way?

但这样我也在设定尺寸。我想避免这种情况。有办法吗?

If there is no way, what would be a good workaround in Swift?

如果没有办法,那么Swift的解决方法是什么?

Thank you!

谢谢!

2 个解决方案

#1


22  

Why not just:

为什么不呢:

titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabel.font.pointSize)

#2


7  

To just make the Font bold without altering the font size you could create an extension like this (which is based off the answer here:

要在不改变字体大小的情况下使字体粗体,您可以创建这样的扩展(这是基于这里的答案:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor()
        .fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor, size: 0)
    }

    func bold() -> UIFont {
        return withTraits(.TraitBold)
    }

}

So that way you could use it like this:

这样你可以像这样使用它:

let titleLabel = UILabel()
titleLabel.font = titleLabel.font.bold() //no need to include size!

Update for Swift 4 syntax:

更新Swift 4语法:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor               
           .withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor!, size: 0)
    }

    func bold() -> UIFont {
        return withTraits(traits: .traitBold)
    }
}

#1


22  

Why not just:

为什么不呢:

titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabel.font.pointSize)

#2


7  

To just make the Font bold without altering the font size you could create an extension like this (which is based off the answer here:

要在不改变字体大小的情况下使字体粗体,您可以创建这样的扩展(这是基于这里的答案:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor()
        .fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor, size: 0)
    }

    func bold() -> UIFont {
        return withTraits(.TraitBold)
    }

}

So that way you could use it like this:

这样你可以像这样使用它:

let titleLabel = UILabel()
titleLabel.font = titleLabel.font.bold() //no need to include size!

Update for Swift 4 syntax:

更新Swift 4语法:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor               
           .withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor!, size: 0)
    }

    func bold() -> UIFont {
        return withTraits(traits: .traitBold)
    }
}