UILabel
iOS
Swift
text formatting
duplicate

Setting UILabel text to bold

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

To set a UILabel's text to bold in iOS, assign a bold UIFont to its font property. The most common approach is UIFont.boldSystemFont(ofSize:) for the system bold font, or UIFont.systemFont(ofSize:weight:) for specific weights like .semibold or .heavy. For dynamic type support, use UIFont.preferredFont(forTextStyle:) with a bold text style. For partial bold text (bolding only certain words), use NSAttributedString with a bold font attribute applied to specific ranges.

System Bold Font

swift
1let label = UILabel()
2label.text = "Hello, World!"
3
4// System bold font at specific size
5label.font = UIFont.boldSystemFont(ofSize: 17)
6
7// Equivalent using weight parameter
8label.font = UIFont.systemFont(ofSize: 17, weight: .bold)

Available Font Weights

swift
1// From lightest to heaviest
2label.font = UIFont.systemFont(ofSize: 17, weight: .ultraLight)
3label.font = UIFont.systemFont(ofSize: 17, weight: .thin)
4label.font = UIFont.systemFont(ofSize: 17, weight: .light)
5label.font = UIFont.systemFont(ofSize: 17, weight: .regular)
6label.font = UIFont.systemFont(ofSize: 17, weight: .medium)
7label.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
8label.font = UIFont.systemFont(ofSize: 17, weight: .bold)
9label.font = UIFont.systemFont(ofSize: 17, weight: .heavy)
10label.font = UIFont.systemFont(ofSize: 17, weight: .black)

Dynamic Type (Accessibility-Friendly)

swift
1// Automatically scales with user's text size preference
2label.font = UIFont.preferredFont(forTextStyle: .headline)  // Bold by default
3label.adjustsFontForContentSizeCategory = true
4
5// Other bold text styles
6label.font = UIFont.preferredFont(forTextStyle: .title1)
7label.font = UIFont.preferredFont(forTextStyle: .title2)
8label.font = UIFont.preferredFont(forTextStyle: .title3)

Custom Font with Dynamic Type Scaling

swift
1// Bold system font that scales with Dynamic Type
2let boldFont = UIFont.boldSystemFont(ofSize: 17)
3label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: boldFont)
4label.adjustsFontForContentSizeCategory = true

Custom Bold Fonts

swift
1// Custom font by name
2label.font = UIFont(name: "Helvetica-Bold", size: 17)
3
4// List available font names for a family
5for name in UIFont.fontNames(forFamilyName: "Helvetica") {
6    print(name)
7    // Helvetica
8    // Helvetica-Bold
9    // Helvetica-BoldOblique
10    // Helvetica-Light
11    // Helvetica-LightOblique
12    // Helvetica-Oblique
13}

Bold Variant of Any Font

swift
1extension UIFont {
2    var bold: UIFont {
3        guard let descriptor = fontDescriptor.withSymbolicTraits(.traitBold) else {
4            return self
5        }
6        return UIFont(descriptor: descriptor, size: 0)  // size 0 = keep original size
7    }
8
9    var italic: UIFont {
10        guard let descriptor = fontDescriptor.withSymbolicTraits(.traitItalic) else {
11            return self
12        }
13        return UIFont(descriptor: descriptor, size: 0)
14    }
15
16    var boldItalic: UIFont {
17        guard let descriptor = fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) else {
18            return self
19        }
20        return UIFont(descriptor: descriptor, size: 0)
21    }
22}
23
24// Usage
25let regularFont = UIFont.systemFont(ofSize: 17)
26label.font = regularFont.bold
27label.font = regularFont.boldItalic

Partial Bold with NSAttributedString

Bold only specific words in a label:

swift
1let fullText = "Welcome to Swift programming"
2let boldText = "Swift"
3
4let attributedString = NSMutableAttributedString(string: fullText)
5let boldFont = UIFont.boldSystemFont(ofSize: 17)
6let regularFont = UIFont.systemFont(ofSize: 17)
7
8// Apply regular font to all
9attributedString.addAttribute(.font, value: regularFont,
10                               range: NSRange(location: 0, length: fullText.count))
11
12// Apply bold to specific range
13if let range = fullText.range(of: boldText) {
14    let nsRange = NSRange(range, in: fullText)
15    attributedString.addAttribute(.font, value: boldFont, range: nsRange)
16}
17
18label.attributedText = attributedString

Helper for Bold Substrings

swift
1extension NSMutableAttributedString {
2    func bold(_ text: String, fontSize: CGFloat = 17) -> NSMutableAttributedString {
3        let boldFont = UIFont.boldSystemFont(ofSize: fontSize)
4        let range = (self.string as NSString).range(of: text)
5        if range.location != NSNotFound {
6            addAttribute(.font, value: boldFont, range: range)
7        }
8        return self
9    }
10}
11
12// Usage
13let attributed = NSMutableAttributedString(string: "Hello World")
14    .bold("World")
15label.attributedText = attributed

Interface Builder / Storyboard

To set bold in Interface Builder:

  1. Select the UILabel in the storyboard
  2. Open the Attributes Inspector
  3. Click the Font field (the "T" icon)
  4. Change Style from "Regular" to "Bold"
  5. Or select a specific weight like "Semibold" or "Heavy"

SwiftUI Equivalent

swift
1// SwiftUI uses .bold() modifier
2Text("Hello, World!")
3    .bold()
4
5// Or font weight
6Text("Hello, World!")
7    .fontWeight(.bold)
8
9// Specific weights
10Text("Hello, World!")
11    .fontWeight(.semibold)
12
13// System font with weight
14Text("Hello, World!")
15    .font(.system(size: 17, weight: .bold))

Common Pitfalls

  • Using UIFont(name:size:) with a wrong font name: If the font name is incorrect (e.g., "Helvetica Bold" instead of "Helvetica-Bold"), the initializer returns nil and the label falls back to the default font. Print UIFont.fontNames(forFamilyName:) to find the exact name.
  • Not enabling adjustsFontForContentSizeCategory: Using preferredFont(forTextStyle:) without setting adjustsFontForContentSizeCategory = true means the font size is set once and never updates when the user changes their text size preference in Settings.
  • Applying attributedText after setting text: Setting label.text clears attributedText, and vice versa. If you set attributedText and then set text, the attributed formatting is lost. Use one or the other, not both.
  • Forgetting size: 0 in UIFont(descriptor:size:): When creating a font from a descriptor (e.g., adding bold trait), passing size: 0 preserves the original font size. Passing a specific size overrides it, which may cause unexpected size changes.
  • Hardcoding font sizes instead of using Dynamic Type: Hardcoded sizes like UIFont.boldSystemFont(ofSize: 17) do not respond to the user's accessibility text size settings. Use UIFontMetrics.scaledFont(for:) to make custom bold fonts support Dynamic Type.

Summary

  • Use UIFont.boldSystemFont(ofSize:) or .systemFont(ofSize:weight: .bold) for bold system fonts
  • Use UIFont.preferredFont(forTextStyle: .headline) with adjustsFontForContentSizeCategory = true for Dynamic Type support
  • Use fontDescriptor.withSymbolicTraits(.traitBold) to get the bold variant of any font
  • Use NSAttributedString with .font attribute to bold specific words within a label
  • In SwiftUI, use .bold() or .fontWeight(.bold) modifiers

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions