爲了只是使字體加粗,而不改變字體大小,你可以創建一個這樣的擴展(這是基於關閉答案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)
}
}
所以這樣你可以像這樣使用它:
let titleLabel = UILabel()
titleLabel.font = titleLabel.font.bold() //no need to include size!
更新夫特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)
}
}
類似的東西:'titleLabel.font = UIFont.boldSystemFont(ofSize:titleLabel.font.pointSize)'? – Larme
[我如何在iPhone/iPad的UILabel上設置粗體和斜體?](http://stackoverflow.com/questions/4713236/how-do-i-set-bold-and-italic-on-uilabel -of-iphone-ipad) –
這可能有助於:[我如何獲得UILabel的字體大小和字體名稱?](http://stackoverflow.com/questions/4866138/how-can-i-get-the -ont-size-and-font-name-of-a-uilabel) – ozgur