Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if NSFont is bold in Swift

Tags:

swift

nsfont

I am writting unit test checking the format of a generated NSAttributedString.

I can extract the font like this:

if let font = attributedString.attributesAtIndex(0, effectiveRange: nil) as? NSFont {
    ...
}

Given this NSFont instance, how can I check if it is bold or not?

like image 833
Martin Delille Avatar asked Jul 15 '16 15:07

Martin Delille


1 Answers

You can check your font traits like this:

let descriptor = font.fontDescriptor
let symTraits = descriptor.symbolicTraits
let traitSet = NSFontTraitMask(rawValue: UInt(symTraits))
let isBold = traitSet.contains(.BoldFontMask))

But I'm not sure if isBold would be true for all seemingly-bold fonts.

like image 115
OOPer Avatar answered Sep 30 '22 00:09

OOPer