UILabel
iOS Development
Text Formatting
Swift
App Design

Bold Non-Bold Text In A Single UILabel?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you can show bold and non-bold text inside a single UILabel by assigning an attributed string to its attributedText property. The usual approach is to build an NSMutableAttributedString, apply different font attributes to different ranges, and then hand the result to the label.

Basic approach with NSMutableAttributedString

swift
1import UIKit
2
3let text = "Name: Ada Lovelace"
4let attributed = NSMutableAttributedString(string: text)
5
6attributed.addAttribute(
7    .font,
8    value: UIFont.boldSystemFont(ofSize: 17),
9    range: NSRange(location: 0, length: 5)
10)
11
12attributed.addAttribute(
13    .font,
14    value: UIFont.systemFont(ofSize: 17),
15    range: NSRange(location: 5, length: text.count - 5)
16)
17
18label.attributedText = attributed

This makes "Name:" bold while leaving the rest in the normal system font.

Why ranges matter

Attributed strings work by attaching formatting attributes to character ranges. That means you need to know which part of the string should be bold and which part should not.

For a fixed string, hard-coded ranges can be fine. For dynamic text, derive the ranges programmatically so the formatting stays correct when the content changes.

Safer range calculation with NSString

Because NSAttributedString uses NSRange, it is often easier to bridge to NSString when calculating ranges for substrings.

swift
1let fullText = "Price: $19.99"
2let nsText = fullText as NSString
3let boldRange = nsText.range(of: "Price:")
4
5let attributed = NSMutableAttributedString(string: fullText)
6attributed.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 17), range: boldRange)
7attributed.addAttribute(.font, value: UIFont.systemFont(ofSize: 17), range: NSRange(location: 0, length: nsText.length))
8
9label.attributedText = attributed

This is usually safer than manually counting characters.

Set a base style first

A good pattern is to assign a base font to the whole string and then override only the bold part.

swift
1let fullText = "Status: Connected"
2let attributed = NSMutableAttributedString(string: fullText)
3
4attributed.addAttribute(
5    .font,
6    value: UIFont.systemFont(ofSize: 17),
7    range: NSRange(location: 0, length: fullText.count)
8)
9
10attributed.addAttribute(
11    .font,
12    value: UIFont.boldSystemFont(ofSize: 17),
13    range: NSRange(location: 0, length: 7)
14)

That keeps the styling predictable when you later add colors, line spacing, or other attributes.

You can style more than boldness

The same attributed string can also control:

  • text color
  • underline
  • kerning
  • paragraph style

So if the label needs mixed formatting, attributedText is the correct general-purpose solution, not a hack just for bolding.

Rebuild the attributed text when the content changes

If the label text is dynamic, rebuild the attributed string whenever the source text changes. Setting label.text later will wipe out the formatting, so the styled string should be treated as the final rendered value rather than a one-time decoration step.

Interface Builder does not replace attributed-string logic

You can configure a label visually in Interface Builder, but mixed bold and non-bold styling inside one label still comes down to attributed text in code or a prepared attributed string resource. For dynamic content, code is usually the clearer and more maintainable option.

Common Pitfalls

  • Setting label.text instead of label.attributedText, which discards the formatting.
  • Hard-coding ranges incorrectly so the wrong characters become bold.
  • Forgetting that NSAttributedString uses NSRange, not Swift Range<String.Index>.
  • Applying only the bold font and forgetting to define a consistent base font for the rest of the text.
  • Rebuilding attributed strings repeatedly in performance-sensitive code when the content rarely changes.

Summary

  • Use NSMutableAttributedString and assign it to UILabel.attributedText.
  • Apply bold and regular fonts to different NSRange values.
  • For dynamic text, calculate ranges programmatically instead of hard-coding them blindly.
  • Set a base style first and then override the emphasized part.
  • Attributed strings are the standard UIKit tool for mixed formatting inside one label.

Course illustration
Course illustration

All Rights Reserved.