How can I get weight of a UILabel?
It seems that fontDescriptor.fontAttributes
won't return all the attributes of a font. Luckily fontDescriptor.object(forKey: .traits)
will, so we can hop onto that.
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]
?? [:]
}
}
and then it's as simple as label.font.weight
(This is fundamentally equivalent to https://stackoverflow.com/a/48688000/1288097 but it uses UIKit
APIs)
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)")
Try following sample font extension with Swift 4. (It needs some improvement for all types of font weights)
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
}
Try with label:
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)")
Here is Apple document for list of Font Weights
The value of this weight is an NSNumber object. The valid value range is from -1.0 to 1.0. The value of 0.0 corresponds to the regular or medium font weight. You can also use a font weight constant to specify a particular weight.