Swift
Attributed String
Text Formatting
iOS Development
Programming Tutorial

Making text bold using attributed string in swift

Master System Design with Codemia

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

Introduction

If you need only part of a string to appear bold in Swift, an attributed string is the right tool. The exact API depends on whether you are working in UIKit or AppKit with NSAttributedString, or in newer Swift and SwiftUI code with the native AttributedString type.

Bold text with NSMutableAttributedString

In UIKit and AppKit code, the most direct approach is to create a mutable attributed string and apply a bold font to the range you want.

swift
1import UIKit
2
3let text = "Total: $42.00"
4let attributed = NSMutableAttributedString(
5    string: text,
6    attributes: [
7        .font: UIFont.systemFont(ofSize: 17)
8    ]
9)
10
11let boldRange = (text as NSString).range(of: "$42.00")
12attributed.addAttribute(
13    .font,
14    value: UIFont.boldSystemFont(ofSize: 17),
15    range: boldRange
16)

You can assign attributed directly to a UILabel, UITextView, or other UIKit view that supports attributed text:

swift
label.attributedText = attributed

This pattern is simple and works well when the bold portion is known as a substring.

Preserve the existing font family when bolding

UIFont.boldSystemFont is convenient, but it changes the font family. If the label already uses a custom font, you usually want the bold version of that same family instead.

swift
1import UIKit
2
3func boldSubstring(_ substring: String, in text: String, font: UIFont) -> NSAttributedString {
4    let attributed = NSMutableAttributedString(
5        string: text,
6        attributes: [.font: font]
7    )
8
9    let range = (text as NSString).range(of: substring)
10    guard range.location != NSNotFound,
11          let boldDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) else {
12        return attributed
13    }
14
15    let boldFont = UIFont(descriptor: boldDescriptor, size: font.pointSize)
16    attributed.addAttribute(.font, value: boldFont, range: range)
17    return attributed
18}
19
20let baseFont = UIFont(name: "AvenirNext-Regular", size: 18)!
21let result = boldSubstring("important", in: "This part is important.", font: baseFont)

This produces bold text without unexpectedly switching to the system font. That detail matters more than many examples admit, especially in apps with a defined visual style.

Use Swift's native AttributedString in newer code

If you are working in SwiftUI or modern Foundation APIs, AttributedString can be cleaner:

swift
1import SwiftUI
2
3var message = AttributedString("Your trial expires tomorrow.")
4
5if let range = message.range(of: "expires tomorrow") {
6    message[range].font = .system(size: 17, weight: .bold)
7}

In SwiftUI, you can render it directly:

swift
Text(message)

This is often nicer than bridging through NSAttributedString unless you specifically need UIKit or AppKit APIs.

Apply bold styling to multiple ranges

If the same text contains several emphasized fragments, apply attributes range by range:

swift
1let text = "Name: Mark\nRole: Engineer"
2let attributed = NSMutableAttributedString(
3    string: text,
4    attributes: [.font: UIFont.systemFont(ofSize: 16)]
5)
6
7for substring in ["Name:", "Role:"] {
8    let range = (text as NSString).range(of: substring)
9    attributed.addAttribute(
10        .font,
11        value: UIFont.boldSystemFont(ofSize: 16),
12        range: range
13    )
14}

This is a good approach for label-value strings, chat highlights, invoice totals, and other structured text where a few fixed fragments need emphasis.

Common Pitfalls

The most common mistake is using String.Index ranges directly with NSAttributedString APIs. Most UIKit and AppKit attribute methods still expect NSRange, so bridge through NSString or convert carefully.

Another common issue is bolding a substring that does not exist. Always guard against NSNotFound, especially when the content comes from user data or localization.

People also accidentally replace a custom font with boldSystemFont. If maintaining the exact font family matters, derive the bold font from the original descriptor instead.

Finally, remember that AttributedString and NSAttributedString are related but not identical. Choose the one that fits the UI framework you are actually using instead of mixing them by habit.

Summary

  • Use NSMutableAttributedString with a .font attribute for UIKit or AppKit code.
  • Convert substring matches to NSRange before applying attributes.
  • Preserve custom font families by deriving a bold descriptor from the existing font.
  • Prefer native AttributedString in modern SwiftUI-friendly code.
  • Guard missing ranges and localization changes so your bolding logic stays safe.

Course illustration
Course illustration

All Rights Reserved.