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

时间:2018-02-08 13:13:57

标签: ios uilabel uifont

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

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

我尝试setValue并且font-weight就在那里但是没有公开的变量来从字体中获得权重。

有可能吗?

5 个答案:

答案 0 :(得分:4)

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

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)")

答案 1 :(得分: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 :(得分: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   对应于常规或中等字体粗细

答案 3 :(得分:1)

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

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

检查docs是否有更多特征。

答案 4 :(得分:1)

似乎fontDescriptor.fontAttributes不会返回 all 字体的属性。幸运的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)

相关问题