Having a UILabel with any font, how can I find out if it is already in bold? Or how can I make it bold? In CSS, I have a font-weight attribute. I would like to have something like that.
All that I have found out so far is that you must set the correct font name. However, this is unreliable. Cochin bold is Cochin-Bold , but the bold version of ArialMT not ArialMT-Bold , but Arial-BoldMT , so obviously adding -Bold not enough. (A bold version of a custom font may also have a completely different name.)
What I can do is find all the fonts for my given font family.
__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 it does not work reliably. I can easily get the BoldItalic version. I could improve my check to avoid this, but this is not a good solution.
Maybe CoreText can help here?
source share