Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether a font is bold/italic on iOS?

Tags:

Given a UIFont or a CTFont, how can I tell whether the font is bold/italic?

like image 538
aryaxt Avatar asked Jul 09 '11 23:07

aryaxt


People also ask

What does bold text look like?

Bold, bold face, or bold font creates the appearance of darker text by applying a thicker stroke weight to the letters. Using bold text in a body paragraph helps emphasize a remark or comment. For example, this is bold text.

What is bold italic font?

A. Bold/italic text is a common way to emphasize the text, such as highlight the text, citation, or people's speech, etc. There are three styles, include bold text, bold-italic text, and italic text. You can change the text style by selecting the options.

What is the difference between bold and italic in text?

The italics are used for weaker emphasis, whereas the bold formatting is for strong emphasis.

Do all fonts support bold?

Answer. No. The font weights available for use are dependent on the font-family itself. Some font families do not have a bold form.


2 Answers

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.

like image 152
Cameron Lowell Palmer Avatar answered Oct 02 '22 21:10

Cameron Lowell Palmer


If you want to do this with Swift 2.0:

extension UIFont {
    var isBold: Bool {
        return fontDescriptor().symbolicTraits.contains(.TraitBold)
    }

    var isItalic: Bool {
        return fontDescriptor().symbolicTraits.contains(.TraitItalic)
    }
}

Usage:

let font: UIFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
if font.isBold {
  print("it's bold..")
}
like image 36
Arjan Avatar answered Oct 02 '22 22:10

Arjan