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
}
查看效果