Setting BOLD font on iOS UILabel Setting BOLD font on iOS UILabel ios ios

Setting BOLD font on iOS UILabel


It's a fishy business to mess with the font names. And supposedly you have an italic font and you wanna make it bold - adding simply @"-Bold" to the name doesn't work. There's much more elegant way:

- (UIFont *)boldFontWithFont:(UIFont *)font{    UIFontDescriptor * fontD = [font.fontDescriptor                fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];    return [UIFont fontWithDescriptor:fontD size:0];}

size:0 means 'keep the size as it is in the descriptor'. You might find useful UIFontDescriptorTraitItalic trait if you need to get an italic font

In Swift it would be nice to write a simple extension:

extension UIFont {    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {        let descriptor = self.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))        return UIFont(descriptor: descriptor, size: 0)    }    func bold() -> UIFont {        return withTraits(.TraitBold)    }    func italic() -> UIFont {        return withTraits(.TraitItalic)    }    func boldItalic() -> UIFont {        return withTraits(.TraitBold, .TraitItalic)    }}

Then you may use it this way:

myLabel.font = myLabel.font.boldItalic()myLabel.font = myLabel.font.bold()myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic)


UPDATE:
Starting with iOS 8, messing with font names is no longer needed. Instead see newer answers that use UIFontDescriptorSymbolicTraits: here and here.


myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];

If you wanna set it programmatically,you must check bold type is support or not in iOS, normally Bold or Italic will have format FontName-Bold, FontName-Italic, FontName-BoldItalic.

Now, write a bold function

-(void)boldFontForLabel:(UILabel *)label{    UIFont *currentFont = label.font;    UIFont *newFont = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Bold",currentFont.fontName] size:currentFont.pointSize];    label.font = newFont;}

Then call it

[self boldFontForLabel:yourLabel];


UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];[myLabel setFont:boldFont];