Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting BOLD font on iOS UILabel

I have assigned a custom font of 'Helvetica' with size 14 already for the text in UILabel using Interface Builder.

I am using reusing the same label at multiple places, but at some place I have to display the text in bold.

Is there any way I can just specify to make the existing font bold instead of creating the whole UIFont again? This is what I do now:

myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14]; 
like image 504
tech_human Avatar asked Sep 18 '13 02:09

tech_human


People also ask

How do I make my text bold on UILabel?

Write the following text in the label “Bold Regular”Double Click on Bold to select it, and then right click on it to see more options. Select font > Bold from that option. It should do the task.

How do I make my font bold on iOS?

Open the Settings app . In the Settings app, select Accessibility from the list. On the Accessibility screen, select Display & Text Size. On the Display & Text Size screen, select Bold Text to set the toggle switch to On.


2 Answers

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) 
like image 186
Maksymilian Wojakowski Avatar answered Sep 26 '22 02:09

Maksymilian Wojakowski


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]; 
like image 32
LE SANG Avatar answered Sep 25 '22 02:09

LE SANG