Setting BOLD font on iOS UILabel

IosObjective CFontsUifont

Ios Problem Overview


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];

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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];

Solution 3 - Ios

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

Solution 4 - Ios

We just need the right font name. I find that iOSFonts.com is the most helpful resource for knowing exactly what name to use.

You can set Bold + ITALIC, by using FONT NAME "Arial-BoldItalicMT"

It works in every Case:

[myLabel setFont:[UIFont fontWithName:@"Arial-BoldItalicMT" size:17]];

Solution 5 - Ios

Extending this answer, in swift:

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

Solution 6 - Ios

I did mine a little differently with Swift

var boldHelveticaFont = UIFont(name: "Helvetica Neue", size: 40)?.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
self.InstructionsTextView.font = UIFont(descriptor: boldHelveticaFont!, size: 40)

Solution 7 - Ios

My contribution with an extension for UILabel 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 that :

let myLabel = UILabel()
myLabel.bold()

Solution 8 - Ios

for "swifters" give a try.

this sample controller will show 4 labels with all the variants.

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 be nice to write an extension for UIFont class.

Solution 9 - Ios

The very old thread, but no one showed how to do it in Swift and keep the previous size:

label.font = UIFont.systemFont(ofSize: label.font!.pointSize, weight: .bold)

Solution 10 - Ios

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

Just add a function (similar to some above) that you can call from anywhere (i.e. without a class) and then you can embolden words within any string, on numerous occasions by calling just ONE LINE of code:

To go in a file like constants.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 just 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)

Solution 11 - Ios

let boldHelveticaFont = UIFont(name: "HelveticaNeue", size: 25)?.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits.traitBold)
label.font = UIFont(descriptor: boldHelveticaFont!, size: 25)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontech_humanView Question on Stackoverflow
Solution 1 - IosMaksymilian WojakowskiView Answer on Stackoverflow
Solution 2 - IosLE SANGView Answer on Stackoverflow
Solution 3 - IosthatzpremView Answer on Stackoverflow
Solution 4 - IosJasmeetView Answer on Stackoverflow
Solution 5 - Ioscscott530View Answer on Stackoverflow
Solution 6 - IosRyanPliskeView Answer on Stackoverflow
Solution 7 - IosKevin ABRIOUXView Answer on Stackoverflow
Solution 8 - IosingcontiView Answer on Stackoverflow
Solution 9 - IosKrzysztof SkrzyneckiView Answer on Stackoverflow
Solution 10 - IosDavid WestView Answer on Stackoverflow
Solution 11 - Iosuser2643679View Answer on Stackoverflow