我怎样才能获得UILabel的重量?

问题描述 投票:4回答:5

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

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

我尝试了po label.font.description和font-weight就在那里但是没有暴露的变量来从字体获得重量。

可能吗?

ios uilabel uifont
5个回答
4
投票

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

4
投票

尝试使用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文档

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


2
投票

似乎没有直接的方法来获得它。作为一种解决方法,您可以获得如下字体重量的指示:

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)。请注意,它不是什么是确切的字体重量,但它可能在某种程度上有助于指示重量:

从字体特征字典访问规范化的权重特征的关键。返回的值是CFNumber,表示标准化权重的-1.0到1.0之间的浮点值。值0.0对应于常规或中等字体粗细。


2
投票

似乎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://stackoverflow.com/a/48688000/1288097,但它使用UIKit API)


1
投票

你可以使用font的符号特征:

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

检查docs更多特征。

© www.soinside.com 2019 - 2024. All rights reserved.