【发布时间】:2020-02-16 08:47:19
【问题描述】:
我正在尝试添加重量为“Heavy”(不是粗体)的系统字体,并尝试将其设为斜体。我看到了其他 stackoverflow 解决方案,但它似乎不起作用。这是我所做的:
let percentageLabel: UILabel = {
let label = UILabel()
label.text = "0"
label.textAlignment = .center
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 32, weight: .heavy, traits: .traitItalic)
return label
}()
堆栈溢出帖子中建议的扩展
extension UIFont {
static func systemFont(ofSize: CGFloat, weight: UIFont.Weight, traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
let font = UIFont.systemFont(ofSize: ofSize, weight: weight)
if let descriptor = font.fontDescriptor.withSymbolicTraits(traits) {
return UIFont(descriptor: descriptor, size: ofSize)
}
return nil
}
}
当我尝试了所有建议的解决方案时,粗体和斜体似乎可以工作,但黑色和斜体不起作用。它仍然呈现为常规斜体。
【问题讨论】: