如何获得UILabel的重量?

Lal*_*mar 5 uilabel uifont ios

情节提要中的用户或以编程方式可以将字体粗细设置为常规,半粗体等。

我想读取任何字体标签的重量。

我试过了,po label.font.description并且font-weight存在,但是没有暴露变量可以从字体中获得权重。

可能吗?

DeF*_*enZ 10

似乎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]
            ?? [:]
    }
}
Run Code Online (Sandbox Code Playgroud)

然后就这么简单 label.font.weight

(这基本上等同于/sf/answers/3408160031/但它使用UIKitAPI)


Kru*_*nal 7

尝试使用 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
}
Run Code Online (Sandbox Code Playgroud)

尝试使用标签:

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)")
Run Code Online (Sandbox Code Playgroud)

这是字体粗细列表的 Apple 文档

这个权重的值是一个 NSNumber 对象。有效值范围是从-1.0 到 1.0。值 0.0 对应于常规或中等字体粗细。您还可以使用字体粗细常量来指定特定的粗细。


bba*_*art 5

要获取字体粗细字符串名称,请使用字体描述符并传入face属性。

斯威夫特4.2

let font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")
Run Code Online (Sandbox Code Playgroud)

迅捷3

let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")
Run Code Online (Sandbox Code Playgroud)

  • 在更高版本的 Swift 中,`UIFontDescriptorFaceAttribute` 已重命名为`UIFontDescriptor.AttributeName.face`(或只是`.face`)。 (4认同)