57

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];
tech_human
  • 6,592
  • 16
  • 65
  • 107
  • Are you asking is there a way to make the actual font file bold without remaking it - or are you asking is there a way to easily make it bold as you set text to that font? – Andrew Sep 18 '13 at 02:24
  • Make it bold without remaking it. I already set my font in the IB, but while drawing the text on the label, I would be passing BOOL to a method to say whether the text is bold or not. If it is bold, it should use the font in IB(without creating again) and just make it bold. – tech_human Sep 18 '13 at 02:28
  • Did you drag and drop any custom font file like .TTF etc.? – kalyani puvvada Sep 18 '13 at 04:24
  • possible duplicate of [I want to make UILabel's text bold](http://stackoverflow.com/questions/4602586/i-want-to-make-uilabels-text-bold) – Praveen Apr 04 '14 at 06:12
  • See here if you don't have the bold variation: http://stackoverflow.com/questions/16047901/how-can-i-both-stroke-and-fill-with-nsattributedstring-w-uilabel – Ferran Maylinch Feb 11 '15 at 18:12

11 Answers11

106

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)
Maksymilian Wojakowski
  • 5,011
  • 3
  • 19
  • 14
  • 10
    And to revert back to regular, use: fontDescriptorWithSymbolicTraits:0 – augustzf May 13 '14 at 08:11
  • Note: Only works on iOS 7, per: https://developer.apple.com/library/ios/documentation/uikit/reference/UIFont_Class/Reference/Reference.html#jumpTo_26 – Matt Mc Jul 25 '14 at 05:14
  • For some reason bold() didn't work for me on iOS8.1.3, testing on a device (it just returns an initialized value), while being fine on a device with iOS 8.3, so I had wrap the function call into an availability check. Not sure why though because the api used should be available in 8.1.3.. – ale84 Nov 10 '15 at 16:18
  • It's just a bug in iOS 8.1.3. Since the API is available, it should work. Nevertheless there's no reason to stay on 8.1.3 when there's higher 8.x version available – Maksymilian Wojakowski Nov 25 '15 at 12:25
  • This seems to revert the font to the system font? So if you use `GillSans` in the label, this code produces a bold font but no longer of the `GillSans` family. – Crashalot Dec 24 '15 at 20:48
  • 1
    I'm adding this here just FYI: I experienced slow performance for fontDescriptorWithSymbolicTraits with custom fonts and different variations (regular, bold, etc.), which caused severe lag in one of my tableviews. Didn't have problems with system fonts though. – ale84 Nov 03 '16 at 13:00
  • 1
    @ale84 - sounds like you did not *cache* the answer. Should be easy enough to write code so that it only has to do the conversion once, rather than once per label it appears in. – ToolmakerSteve Mar 08 '17 at 04:23
  • 1
    @ToolmakerSteve yes caching could be one way to resolve the problem. Actually for me the hard part was figuring out what was causing the lag, because I didn't expect it to be this... – ale84 Mar 09 '17 at 14:00
87

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];
Community
  • 1
  • 1
LE SANG
  • 10,955
  • 7
  • 59
  • 78
36
UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[myLabel setFont:boldFont];
thatzprem
  • 4,697
  • 1
  • 33
  • 41
  • Swift 3.0: UIFont.systemFont(ofSize: 20, weight: UIFontWeightSemibold) – Josh O'Connor Jan 05 '17 at 22:58
  • 1
    Good answer, +1 ;). I'm using xcode 10.2 and this will also work: `UIFont *boldFont = [UIFont systemFontOfSize:[UIFont systemFontSize] weight:UIFontWeightHeavy];` and if you want to go back to normal: `UIFont *normalFont = [UIFont systemFontOfSize:[UIFont systemFontSize] weight:UIFontWeightRegular];` To apply: `self.btnSave.titleLabel.font = normalFont;` – Sam Apr 23 '19 at 12:14
6

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]];
Jasmeet
  • 1,522
  • 2
  • 22
  • 41
6

Extending this answer, in swift:

extension UIFont {
    func bold() -> UIFont {
        let descriptor = self.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
        return UIFont(descriptor: descriptor, size: 0)
    }
}
Community
  • 1
  • 1
cscott530
  • 1,658
  • 16
  • 25
  • 1
    It adds a method right to UIFont. so in this case it would be like: `let boldFont = originalFont.bold()` – cscott530 Mar 18 '15 at 15:01
  • 1
    A getter property might be a better option here since a function can return a value or perform some action. That ambiguity can be clarified with a property and saves you from typing the parenthesis. – Alexander Jul 28 '15 at 18:37
4

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)
RyanPliske
  • 391
  • 3
  • 7
2

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()
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
1

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.

ingconti
  • 10,876
  • 3
  • 61
  • 48
1

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)
Krzysztof Skrzynecki
  • 2,345
  • 27
  • 39
0

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)
David West
  • 1,550
  • 1
  • 18
  • 31
0
let boldHelveticaFont = UIFont(name: "HelveticaNeue", size: 25)?.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits.traitBold)
label.font = UIFont(descriptor: boldHelveticaFont!, size: 25)
user2643679
  • 706
  • 12
  • 18