用户可以通过Storyboard或编程方式设置字体的粗细,如常规、半粗等。
我想读取任何字体标签的字重。
我尝试了po label.font.description
,字重也在那里,但没有外部变量可以获取字体的字重。
这可行吗?
看起来 fontDescriptor.fontAttributes
不会返回字体的所有属性。幸运的是,fontDescriptor.object(forKey: .traits)
可以返回所有属性,所以我们可以使用这个方法。
extension UIFont {
var weight: UIFont.Weight {
guard let weightNumber = traits[.weight] as? NSNumber else { return .regular }
let weightRawValue = CGFloat(weightNumber.doubleValue)
let weight = UIFont.Weight(rawValue: weightRawValue)
return weight
}
private var traits: [UIFontDescriptor.TraitKey: Any] {
return fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any]
?? [:]
}
}
然后,它就像 label.font.weight
这样简单。
(这与 https://dev59.com/_6jka4cB1Zd3GeqPFuSs#48688000 原理上是等价的,但它使用了 UIKit
的API)
return UIFont.monospacedDigitSystemFont(ofSize: self.pointSize, weight: self.weight)
。 - JLundell要获取字体的加粗程度字符串名称,使用字体描述符并传递face属性。
Swift 4.2
let font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")
Swift 3
let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")
UIFontDescriptorFaceAttribute
在Swift的较新版本中已更名为UIFontDescriptor.AttributeName.face
(或简写为.face
)。 - Dafydd Williams尝试使用Swift 4编写以下示例字体扩展(它需要改进以适用于所有字重类型)
extension UIFont {
func getFontWeight() -> UIFont.Weight {
let fontAttributeKey = UIFontDescriptor.AttributeName.init(rawValue: "NSCTFontUIUsageAttribute")
if let fontWeight = self.fontDescriptor.fontAttributes[fontAttributeKey] as? String {
switch fontWeight {
case "CTFontBoldUsage":
return UIFont.Weight.bold
case "CTFontBlackUsage":
return UIFont.Weight.black
case "CTFontHeavyUsage":
return UIFont.Weight.heavy
case "CTFontUltraLightUsage":
return UIFont.Weight.ultraLight
case "CTFontThinUsage":
return UIFont.Weight.thin
case "CTFontLightUsage":
return UIFont.Weight.light
case "CTFontMediumUsage":
return UIFont.Weight.medium
case "CTFontDemiUsage":
return UIFont.Weight.semibold
case "CTFontRegularUsage":
return UIFont.Weight.regular
default:
return UIFont.Weight.regular
}
}
return UIFont.Weight.regular
}
尝试使用标签:
let label = UILabel()
var fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.black)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.heavy)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.ultraLight)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.thin)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.light)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
fontWeight = label.font.getFontWeight()
print("fontWeight - \(fontWeight)")
看起来没有直接的方法可以获得它。作为一种解决方案,您可以按如下方式获取字体的重量指示:
let labelFont = label.font as CTFont
if let fontTraits = CTFontCopyTraits(labelFont) as? [CFString: CFNumber], let fontWeight = fontTraits[kCTFontWeightTrait] {
print(fontWeight)
}
UIFont已被转换为CTFont,通过使用CTFontCopyTraits(_:)
生成CFDictionary(包含kCTFontWeightTrait的值)。请注意,它可能无法确定确切的字体重量,但对于指示重量可能有所帮助:
从字体特征字典中访问标准化权重特征的关键字。返回的值是表示标准化权重的-1.0到1.0之间的浮点值的CFNumber。值0.0对应于常规或中等字体重量。
// is true when font is bold
label.font.fontDescriptor.symbolicTraits.contains(UIFontDescriptorSymbolicTraits.traitBold)
查看文档以获取更多特征。
label.font.UIFontWeight
- Cy-4AH