我正在尝试添加一种系统字体,它的重量是“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
}()
根据Stack Overflow上的一篇帖子所建议的扩展
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
}
}
问题在于当我尝试了所有建议的解决方案后,加粗和斜体效果可以实现,但黑体和斜体效果无法实现。它仍然被呈现为普通斜体。