Given a UIFont or a CTFont, how can I tell whether the font is bold/italic?
Asked
Active
Viewed 9,112 times
4 Answers
45
iOS7 Font Descriptor
No reason to use Core Text, you can simply ask UIFont for the fontDescriptor.
UIFont *font = [UIFont boldSystemFontOfSize:17.0f];
UIFontDescriptor *fontDescriptor = font.fontDescriptor;
UIFontDescriptorSymbolicTraits fontDescriptorSymbolicTraits = fontDescriptor.symbolicTraits;
BOOL isBold = (fontDescriptorSymbolicTraits & UIFontDescriptorTraitBold) != 0;
Going forward this is probably the easiest way to ask about the traits of a font.

Cameron Lowell Palmer
- 21,528
- 7
- 125
- 126
-
Don't cast `uint32_t` to `BOOL`, it will cause you problems. Use `BOOL isBold = (fontDescriptorSymbolicTraits & UIFontDescriptorTraitBold) != 0` or `BOOL isBold = !!(fontDescriptorSymbolicTraits & UIFontDescriptorTraitBold)` instead. – Jeffery Thomas Nov 24 '15 at 03:37
39
If you want to do this with Swift:
extension UIFont {
var isBold: Bool {
return fontDescriptor.symbolicTraits.contains(.traitBold)
}
var isItalic: Bool {
return fontDescriptor.symbolicTraits.contains(.traitItalic)
}
}
Usage:
let font: UIFont = UIFont.preferredFont(forTextStyle: .headline)
if font.isBold {
print("it's bold..")
}

Arjan
- 16,210
- 5
- 30
- 40
-
13@JanGreve please reconsider your downvote, Answering old questions when there's new solutions available is a good thing, and I added Swift tag for clarity. – Arjan Feb 25 '16 at 18:44
-
4I removed the swift tag again. There is no way the author could've meant to ask for swift, so your edit clearly conflicts with the author's intend. Other than that, you are right; a new objective-c technique would have been fine. – Tobi Nary Feb 25 '16 at 18:47
-
3Note: this is currently being [discussed on meta](http://meta.stackoverflow.com/questions/317716/are-only-tags-allowed-that-the-author-could-have-intended) @Jan – Shog9 Feb 25 '16 at 19:42
35
Looking at the font's name won't always work. Consider the font "Courier Oblique" (which is italic) or "HoeflerText-Black" (which is bold), Neither of those contain "bold" or "italic" in their names.
Given a font as a CTFontRef
, the proper way to determine whether it's bold or italic is to use the CTFontGetSymbolicTraits
function:
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Courier Oblique", 10, NULL);
CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
BOOL isItalic = ((traits & kCTFontItalicTrait) == kCTFontItalicTrait);
BOOL isBold = ((traits & kCTFontBoldTrait) == kCTFontBoldTrait);
NSLog(@"Italic: %i Bold: %i", isItalic, isBold);
CFRelease(font);

omz
- 53,243
- 5
- 129
- 141
-
Thanks, I think this is a really clean way of doing this. One more question. Let's say I have a CTFont, how can I make it bold? – aryaxt Jul 10 '11 at 16:34
-
1
-
```isItalic``` doesn't work with Superclarendon, Marion and Gill Sans fonts. – Shmidt Sep 13 '14 at 10:10
11
Answer for Swift 3/4 based on Arjan's answer:
extension UIFont {
var isBold: Bool {
return fontDescriptor.symbolicTraits.contains(.traitBold)
}
var isItalic: Bool {
return fontDescriptor.symbolicTraits.contains(.traitItalic)
}
}

John Rogers
- 2,192
- 19
- 29