制作 UIFont Black/Heavy 和斜体
Making UIFont Black/Heavy and Italic
如何为标签设置字体 heavy AND italic?
来自 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)
}
}
编辑:
我的"real"代码:
@IBOutlet weak var labelCurrentDate: UILabel! {
didSet {
labelCurrentDate.font = UIFont(descriptor: UIFont.systemFont(ofSize: 32, weight: .black).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 32)
labelCurrentDate.textColor = colorLightMainText
}
}
我的结果:
如您所见,我的标签文字粗细不是 "black" - 这是正常的字体粗细,但我希望它是黑色的。
我是这样测试的:
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
}
这是我看到的:
所以在我看来,我们在斜体字体样式中获得了所有不同的粗细。
如何为标签设置字体 heavy AND italic?
来自 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)
}
}
编辑:
我的"real"代码:
@IBOutlet weak var labelCurrentDate: UILabel! {
didSet {
labelCurrentDate.font = UIFont(descriptor: UIFont.systemFont(ofSize: 32, weight: .black).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 32)
labelCurrentDate.textColor = colorLightMainText
}
}
我的结果:
如您所见,我的标签文字粗细不是 "black" - 这是正常的字体粗细,但我希望它是黑色的。
我是这样测试的:
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
}
这是我看到的:
所以在我看来,我们在斜体字体样式中获得了所有不同的粗细。