Given a UIFont or a CTFont, how can I tell whether the font is bold/italic?
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.
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.
The italics are used for weaker emphasis, whereas the bold formatting is for strong emphasis.
Answer. No. The font weights available for use are dependent on the font-family itself. Some font families do not have a bold form.
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.
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..")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With