Search code examples
iosuilabeluifont

How can I get weight of a UILabel?


User from storyboard or programatically can set font weight as regular, semi bold etc.

I want to read weight for any font label.

I tried po label.font.description and font-weight is there and but there is no exposed variable to get weight from font.

Is it possible?


Solution

  • To get the font weight string name, use the font descriptor and pass in the face attribute.

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