ios 如何获取UILabel的权重?

q0qdq0h2  于 2023-11-19  发布在  iOS
关注(0)|答案(5)|浏览(140)

用户从故事板或图解可以设置字体粗细为常规,半粗体等。
我想读取任何字体标签的重量。
我尝试了po label.font.description和font-weight是有,但没有暴露的变量从字体获得重量。
这可能吗?

bnlyeluc

bnlyeluc1#

看起来fontDescriptor.fontAttributes不会返回字体的所有属性。幸运的是fontDescriptor.object(forKey: .traits)会,所以我们可以跳到它上面。
较新的Swift版本:

extension UIFont {
    var weight: UIFont.Weight {
        guard let weightNumber = traits[.weight] as? Double else { return .regular }
        let weight = UIFont.Weight(rawValue: weightNumber)
        return weight
    }

    private var traits: [UIFontDescriptor.TraitKey: Any] {
        return fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any]
            ?? [:]
    }
}

字符串
较旧的Swift版本:

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一样简单
(This基本上等同于https://stackoverflow.com/a/48688000/1288097,但它使用UIKit API)

deikduxw

deikduxw2#

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

字符串

  • 斯威夫特3号酒店,纽约**
let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")

zzzyeukh

zzzyeukh3#

尝试使用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列表文档
此权重的值为NSNumber对象。有效值范围为**-1.0到1.0**。值0.0对应于常规或中等字体权重。您也可以使用字体权重常量来指定特定权重。

uhry853o

uhry853o4#

似乎没有直接的方法来获得它.作为一种变通方法,你可以得到什么是字体的重量如下指示:

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

字符串
UIFont被转换为CTFont,它生成包含kCTFontWeightTrait值的CFDictionary(通过使用CTFontCopyTraits(_:))。请注意,它不会知道确切的字体粗细,但它可能在某种程度上有助于指示粗细:
用于从字体特征字典中访问归一化权重特征的键。返回的值是CFNumber,表示归一化权重的-1.0到1.0之间的浮点值。值0.0 对应于常规或中等字体粗细*。

azpvetkf

azpvetkf5#

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

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

字符串
查看docs以获取更多特征。

相关问题