I'm trying to figure out how to best bold and un-bold a UILabel with a font that was defined in the Interface Builder.
I know I can do this, for example:
[myLabel setFont:[UIFont boldSystemFontOfSize:14.0]];
But then I'm forcing the font to potentially different (both in style and size) than the way it was designed in IB.
I'm looking for something that essentially does this:
[myLabel setBold:TRUE];
or False, as the case may be.
Is there a way to do this?
Unfortunately, there's no real concept of "Bold" in UIKit. If you ever try setting part of an attributed string to "Bold", you are actually selecting the bold font variant in the same font family.
You could so something semihackish like this:
@implementation UIFont (BoldVariant)
- (UIFont *)boldVariant
{
for (NSString *fontName in [UIFont fontNamesForFamilyName:self.familyName]) {
if ([fontName hasSuffix:@"-Bold"]) {
return [UIFont fontWithName:fontName size:self.pointSize];
}
}
// If we couldn't find it, return the same font.
return self;
}
@end
This assumes that the font follows the standard naming scheme. It also assumes that fontNamesForFamilyName:
returns any values. I noticed that with the system font, it returns an empty array.