如何获取UILabel的重量?

12

用户可以通过Storyboard或编程方式设置字体的粗细,如常规、半粗等。

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

我尝试了po label.font.description,字重也在那里,但没有外部变量可以获取字体的字重。

这可行吗?


label.font.UIFontWeight - Cy-4AH
我认为不可能获取字体的重量,你只能检查它是否加粗。 - Prashant Tukadiya
1
也许这个会有用:-https://developer.apple.com/documentation/uikit/uifontdescriptor.traitkey - AshvinGudaliya
5个回答

16

看起来 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://dev59.com/_6jka4cB1Zd3GeqPFuSs#48688000 原理上是等价的,但它使用了 UIKit 的API)


优雅。我在扩展中添加了一个变量,用于将IB定义的系统字体转换为其等宽数字等效字体,这是我一直在努力解决的问题。谢谢。无法在此处编写代码,但核心内容是:return UIFont.monospacedDigitSystemFont(ofSize: self.pointSize, weight: self.weight) - JLundell

12

要获取字体的加粗程度字符串名称,使用字体描述符并传递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)")

4
UIFontDescriptorFaceAttribute在Swift的较新版本中已更名为UIFontDescriptor.AttributeName.face(或简写为.face)。 - Dafydd Williams

10

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

这是苹果文档,其中包含字体粗细列表。
此权重的值是NSNumber对象。有效值范围为-1.0至1.0。值为0.0对应于常规或中等字体重量。您还可以使用字体重量常数来指定特定的重量。

3

看起来没有直接的方法可以获得它。作为一种解决方案,您可以按如下方式获取字体的重量指示:

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的值)。请注意,它可能无法确定确切的字体重量,但对于指示重量可能有所帮助:

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


2
你可以尝试调整字体的符号特征:
// is true when font is bold
label.font.fontDescriptor.symbolicTraits.contains(UIFontDescriptorSymbolicTraits.traitBold)

查看文档以获取更多特征。


2
当我检查粗体和斜体样式时,这个是有效的。但是对于半粗体,没有UIFontDescriptorSymbolicTraits。 - Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接