我正在尝试添加粗细为“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
}()
Run Code Online (Sandbox Code Playgroud)
堆栈溢出帖子中建议的扩展
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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试所有建议的解决方案时,粗体和斜体似乎有效,但黑色和斜体不起作用。它仍然呈现为常规斜体。