如何获得 UILabel 的权重?

How can I get weight of a UILabel?

故事板中的用户或以编程方式可以将字体粗细设置为常规、半粗体等

我想读取任何字体标签的粗细。

我试过 po label.font.description 那里有字体粗细,但是没有暴露的变量可以从字体中获取粗细。

可能吗?

您可以尝试使用字体的符号特征:

// is true when font is bold
label.font.fontDescriptor.symbolicTraits.contains(UIFontDescriptorSymbolicTraits.traitBold)

检查 docs 以获得更多特征。

好像没有直接获取的方法。作为解决方法,您可以获得如下字体粗细的指示:

let labelFont = label.font as CTFont
if let fontTraits = CTFontCopyTraits(labelFont) as? [CFString: CFNumber], let fontWeight = fontTraits[kCTFontWeightTrait] {
    print(fontWeight)
}

UIFont has been casted as CTFont, which generates CFDictionary (by using CTFontCopyTraits(_:)) that contains the value of kCTFontWeightTrait。请注意,它不会是确切的字体粗细,但它可能以某种方式对指示粗细是多少有用:

Key to access the normalized weight trait from the font traits dictionary. The value returned is a CFNumber representing a float value between -1.0 and 1.0 for normalized weight. The value of 0.0 corresponds to the regular or medium font weight.

要获取字体粗细字符串名称,请使用字体描述符并传入 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)")

尝试使用 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)")

这是 Font Weights

列表的 Apple 文档

The value of this weight is an NSNumber object. The valid value range is from -1.0 to 1.0. The value of 0.0 corresponds to the regular or medium font weight. You can also use a font weight constant to specify a particular weight.

好像fontDescriptor.fontAttributes不会return所有一个字体的属性。幸运的是 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

一样简单

(这基本上等同于 ,但它使用 UIKit API)