Having a UILabel
with any font, how can I find out if it is already bold? Or how can I make it bold? In CSS, I have a font-weight
attribute. I would like to have something similar.
Everything I found out so far is that you have to set the proper font name. However, this is unreliable. The bold version of Cochin
is Cochin-Bold
, but the bold version of ArialMT
is not ArialMT-Bold
but Arial-BoldMT
, so it obviously does not suffice to append -Bold
. (The bold version of a custom font could also have a totally different name).
What I can do is finding all fonts for the family of my given font.
__block UIFont *font = myLabel.font;
[[UIFont fontNamesForFamilyName:font.familyName] enumerateObjectsUsingBlock:^(NSString *fontName, NSUInteger idx, BOOL *stop) {
if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound) {
font = [UIFont fontWithName:fontName size:font.pointSize];
*stop = YES;
}
}];
myLabel.font = font;
But this does not work reliably. I can easily get a BoldItalic
version. I could improve my check to avoid this, but it is not really a good solution.
Maybe CoreText can help here?
Here's what you need to know about formatting text with shortcuts in Word: To make your selected text bold or start writing text in bold, press the Crtl + B keys on your keyboard. To make your selected text italic or start writing text in italic, press the Ctrl + I keys on your keyboard.
Type the keyboard shortcut: CTRL+B.
Use the <em> tag The “em” in <em> literally stands for emphasis. Browsers will, by default, make italicize text that is wrapped in HTML <em> tags. Imagine the sound of that sentence, where the reader is emphasizing that word giving the sentence a different feel that if they didn't.
Maybe CoreText can help here?
CoreText uses its own font system, CTFont. If you're using that, you can do what you want:
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
I suppose you could then get the name of the derived bold font:
CFStringRef boldName = CTFontCopyPostScriptName(boldFont);
...and use it to create a new UIFont:
UIFont *ret = [UIFont fontWithName:(NSString *)boldName size:size];
I don't know how quick this would be, but you could do it on app launch then cache the names.
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