How do I set bold and italic on UILabel
of iPhone/iPad?
I searched the forum but nothing helped me. Could anyone help me?
19 Answers
sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];
There is a list of font names that you can set in place of 'fontWithName' attribute.The link is here
Don't try to play with the font names. Using the font descriptor you need no names:
UILabel * label = [[UILabel alloc] init]; // use your label object instead of this
UIFontDescriptor * fontD = [label.font.fontDescriptor
fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
| UIFontDescriptorTraitItalic];
label.font = [UIFont fontWithDescriptor:fontD size:0];
size:0
means 'keep the size as is'
With Swift try the following extension:
extension UIFont {
func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
let descriptor = self.fontDescriptor()
.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
return UIFont(descriptor: descriptor, size: 0)
}
func boldItalic() -> UIFont {
return withTraits(.TraitBold, .TraitItalic)
}
}
Then you may use it this way:
myLabel.font = myLabel.font.boldItalic()
or even add additional traits like Condensed:
myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic)
Update for Swift 4:
extension UIFont {
var bold: UIFont {
return with(traits: .traitBold)
} // bold
var italic: UIFont {
return with(traits: .traitItalic)
} // italic
var boldItalic: UIFont {
return with(traits: [.traitBold, .traitItalic])
} // boldItalic
func with(traits: UIFontDescriptorSymbolicTraits) -> UIFont {
guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
return self
} // guard
return UIFont(descriptor: descriptor, size: 0)
} // with(traits:)
} // extension
Use it as follows:
myLabel.font = myLabel.font.bold
or
myLabel.font = myLabel.font.italic
or
myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])
Update for Swift 5
extension UIFont {
var bold: UIFont {
return with(.traitBold)
}
var italic: UIFont {
return with(.traitItalic)
}
var boldItalic: UIFont {
return with([.traitBold, .traitItalic])
}
func with(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
guard let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits)) else {
return self
}
return UIFont(descriptor: descriptor, size: 0)
}
func without(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
guard let descriptor = self.fontDescriptor.withSymbolicTraits(self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptor.SymbolicTraits(traits))) else {
return self
}
return UIFont(descriptor: descriptor, size: 0)
}
}
I made a variation of the response of maksymilian wojakowski where you can add or remove a trait(s)
extension UIFont {
func withTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
let descriptor = self.fontDescriptor
.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits))
return UIFont(descriptor: descriptor!, size: 0)
}
func withoutTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
let descriptor = self.fontDescriptor
.withSymbolicTraits( self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptorSymbolicTraits(traits)))
return UIFont(descriptor: descriptor!, size: 0)
}
func bold() -> UIFont {
return withTraits( .traitBold)
}
func italic() -> UIFont {
return withTraits(.traitItalic)
}
func noItalic() -> UIFont {
return withoutTraits(.traitItalic)
}
func noBold() -> UIFont {
return withoutTraits(.traitBold)
}
}
exemple
label.font = label.font.italic().bold()
it useful when reusing cell and you want to remove the italic you put on a label in a previous cell
I recently wrote a blog post about the restrictions of the UIFont API and how to solve it. You can see it at here
With the code I provide there, you can get your desired UIFont as easy as:
UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style: italic;"];
And then set it to your UILabel (or whatever) with:
myLabel.font = myFont;
As has already been mentioned in these answers, you just need the right font name. I find that iOSFonts.com is the most helpful resource for knowing exactly what name to use.
Example Bold text:
UILabel *titleBold = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
UIFont* myBoldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[titleBold setFont:myBoldFont];
Example Italic text:
UILabel *subTitleItalic = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 200, 30)];
UIFont* myItalicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[subTitleItalic setFont:myItalicFont];
Although the answer provided by @tolbard is amazing and works well!
I feel creating an extension for something that can be achieved in just a line of code, would be an over kill.
You can get bold as well italic styling for the same text in your label
by setting up the font property using UIFontDescriptor
as shown below in the example below using Swift 4.0:
label.font = UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 12)
Other options include:
traitLooseLeading
traitTightLeading
traitUIOptimized
traitVertical
traitMonoSpace
traitCondensed
traitExpanded
For more information on what those symbolic traits mean? visit here
Updating Maksymilian Wojakowski's awesome answer for swift 3
extension UIFont {
func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont? {
guard let descriptorL = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) else{
return nil
}
return UIFont(descriptor: descriptorL, size: 0)
}
func boldItalic() -> UIFont? {
return withTraits(traits: .traitBold, .traitItalic)
}
}