BOLD font setup on iOS UILabel

I assigned a custom font “Helvetica” with a size of 14 already for text in UILabel using Interface Builder.

I am using reuse of the same label in several places, but in some place I have to display the text in bold.

Is there a way I can just indicate to make the existing font bold instead of creating the whole UIFont again? This is what I am doing now:

myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14]; 
+54
ios objective-c fonts uifont
Sep 18 '13 at 2:20
source share
9 answers

UPDATE:
Starting with iOS 8, the use of font names is no longer required. Instead, check out the newer answers that use UIFontDescriptorSymbolicTraits : here and here .




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

If you want to install it programmatically, you should check whether the bold type is supported or not in iOS, usually Bold or Italic will have the format FontName-Bold, FontName-Italic, FontName-BoldItalic.

Now write 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 him

 [self boldFontForLabel:yourLabel]; 
+81
Sep 18 '13 at 2:25
source share
— -

It’s a fish business to mess with font names. And, presumably, you have an italic font and you want to make it bold - just adding @"-Bold" to the name will not work. There's a 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 handle." You can find a useful feature of UIFontDescriptorTraitItalic 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 can use it as follows:

 myLabel.font = myLabel.font.boldItalic() myLabel.font = myLabel.font.bold() myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic) 
+102
Feb 14 '14 at 10:28
source share
 UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; [myLabel setFont:boldFont]; 
+33
Sep 18 '13 at 6:19 06:19
source share

Extending this answers fast:

 extension UIFont { func bold() -> UIFont { let descriptor = self.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold) return UIFont(descriptor: descriptor, size: 0) } } 
+6
Nov 12 '14 at 17:45
source share

We just need the correct font name. I find that iOSFonts.com is the most useful resource for knowing exactly which name to use.

You can install Bold + ITALIC using FONT NAME "Arial-BoldItalicMT"

It works in every case:

 [myLabel setFont:[UIFont fontWithName:@"Arial-BoldItalicMT" size:17]]; 
+5
Dec 30 '14 at 11:09
source share

I did a little different with Swift

 var boldHelveticaFont = UIFont(name: "Helvetica Neue", size: 40)?.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold) self.InstructionsTextView.font = UIFont(descriptor: boldHelveticaFont!, size: 40) 
+4
Feb 12 '15 at 20:30
source share

for "swifters" try.

This sample controller will display 4 shortcuts with all options.

 import UIKit class ViewController: UIViewController { var labl: UILabel? var labl2: UILabel? var labl3: UILabel? var labl4: UILabel? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let font = UIFont.systemFontOfSize(32) labl = UILabel(frame: CGRectMake(20,20,400, 45)) labl!.text = "Attention:" labl!.font = font labl2 = UILabel(frame: CGRectMake(20,60,400, 45)) labl2!.text = "Attention:" labl2!.font = bold(font) labl3 = UILabel(frame: CGRectMake(20,100,400, 45)) labl3!.text = "Attention:" labl3!.font = italic(font) labl4 = UILabel(frame: CGRectMake(20,140,400, 45)) labl4!.text = "Attention:" labl4!.font = boldAndItalic(font) self.view.addSubview(labl!) self.view.addSubview(labl2!) self.view.addSubview(labl3!) self.view.addSubview(labl4!) } // nice to write an extension... func bold(font: UIFont) -> UIFont { let descriptor = font.fontDescriptor().fontDescriptorWithSymbolicTraits([.TraitBold]) return UIFont(descriptor: descriptor, size: 0) } func boldAndItalic(font: UIFont) -> UIFont { let descriptor = font.fontDescriptor().fontDescriptorWithSymbolicTraits([.TraitBold, .TraitItalic]) return UIFont(descriptor: descriptor, size: 0) } func italic(font: UIFont) -> UIFont { let descriptor = font.fontDescriptor().fontDescriptorWithSymbolicTraits([.TraitItalic]) return UIFont(descriptor: descriptor, size: 0) } } 

wulld is nice to write an extension for the UIFont class.

+1
Mar 16 '16 at 8:57
source share

You probably don't need an “extension” as such if you want this for any custom fonts ...

Just add a function (similar to the part above) that you can call from anywhere (i.e. without a class), and then you can repeatedly spoof words on any line, calling only ONE LINE of code:

To go to a file like constantants.swift:

 import Foundation import UIKit func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString { let nonBoldFontAttribute = [NSFontAttributeName:font!] let boldFontAttribute = [NSFontAttributeName:boldFont] let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute) boldString.addAttributes(boldFontAttribute, range: fullString.rangeOfString(boldPartOfString as String)) return boldString } 

Then you can simply call this one line of code for any UILabel:

 self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldSearchFont!) //Mark: Albeit that you've had to define these somewhere: let normalFont = UIFont(name: "INSERT FONT NAME", size: 15) let boldFont = UIFont(name: "INSERT BOLD FONT", size: 15) 
0
May 21 '16 at 1:13
source share

My contribution with extension for UILabel has been updated for Swift 4:

 extension UILabel{ func bold() -> UILabel { if let descriptor = self.font.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits.traitBold){ self.font = UIFont(descriptor: descriptor, size: 0) } return self } } 

Just call .bold () like this:

 let myLabel = UILabel() myLabel.bold() 
0
Jun 07 '19 at 12:37
source share



All Articles