如何为标签设置字体 沉重 AND 楷体?
从SO的例子不工作。
labelCurrentDate.font = UIFont(descriptor: UIFont.systemFont(ofSize: 32, weight: .black).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 32)
也没有工作。
extension UIFont {
static func systemFontItalic(size fontSize: CGFloat = 17.0, fontWeight: UIFont.Weight = .regular) -> UIFont {
let font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
return UIFont(descriptor: font.fontDescriptor.withSymbolicTraits(.traitItalic)!, size: fontSize)
}
}
编辑:
我的 "真实 "代码
@IBOutlet weak var labelCurrentDate: UILabel! {
didSet {
labelCurrentDate.font = UIFont(descriptor: UIFont.systemFont(ofSize: 32, weight: .black).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 32)
labelCurrentDate.textColor = colorLightMainText
}
}
我的结果
如你所见,我的标签文字权重不是 "黑色"--它是一个正常的字体权重,但我希望它是黑色的。
我这样测试了一下。
let weights : [UIFont.Weight] = [
.ultraLight, .thin, .light, .regular,
.medium, .semibold, .bold, .heavy, .black
]
var top : CGFloat = 25
for weight in weights {
let desc = UIFont.systemFont(ofSize: 32).fontDescriptor.addingAttributes([
UIFontDescriptor.AttributeName.traits: [
UIFontDescriptor.TraitKey.weight:
weight.rawValue,
UIFontDescriptor.TraitKey.symbolic:
UIFontDescriptor.SymbolicTraits.traitItalic.rawValue
]
])
let lab = UILabel()
lab.font = UIFont(descriptor: desc, size: 0)
lab.text = "This is a test"
lab.sizeToFit()
lab.frame.origin = CGPoint(x: 20, y: top)
self.view.addSubview(lab)
top += lab.bounds.height + 8
}
这就是我看到的结果
所以在我看来,我们得到的是所有不同权重的斜体字体。