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?