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
In Swift, the usual way to make only part of a string bold is to build an attributed string and apply a bold font to a specific range. The core idea is simple, but the important details are range calculation, preserving the base font for the rest of the text, and choosing between NSAttributedString and the newer Swift AttributedString APIs.
Use NSMutableAttributedString in UIKit
The classic UIKit approach is to create a mutable attributed string, then apply a bold font attribute to the part you want emphasized.
Then assign it to a label:
This is the most common solution in UIKit because UILabel, UITextView, and other text-rendering classes already understand NSAttributedString.
Find the Range Safely
The biggest practical issue is usually not the font attribute. It is finding the correct range. Because UIKit APIs frequently use NSRange, searching through NSString is often the simplest option when you need compatibility with attributed APIs.
If the substring might not exist, check range.location != NSNotFound before applying attributes. Otherwise you risk a crash when you try to style an invalid range.
Keep the Rest of the Text Styled Predictably
If you set only the bold range and never assign a base font to the rest, the final appearance can depend on label defaults or other attributes already attached to the string. A stable pattern is:
- apply base attributes to the full range
- override just the bold segment
That makes the styling predictable and easier to extend later with color, kerning, or paragraph style.
Use Swift AttributedString on Newer Platforms
On newer Apple platforms, Swift's native AttributedString is often more pleasant to work with.
This API feels more Swifty and avoids some of the awkward NSRange bridging. It is a good option when your deployment target and UI stack already support it comfortably.
Bold by Meaning, Not Only by Fixed Position
If the bold portion is dynamic, search for the meaningful substring instead of hard-coding numeric positions. For example, bold the username, price, or status string after the text is assembled. That makes the code more resilient when wording changes.
If the text contains repeated substrings and only one should be bold, be explicit about which occurrence you mean. Simple range(of:) logic always finds the first match unless you search differently.
Common Pitfalls
- Applying the bold attribute to the wrong range because of incorrect string indexing.
- Forgetting to set a base font for the non-bold portion of the text.
- Using
NSRangeon a SwiftStringincorrectly instead of bridging throughNSStringwhen needed. - Assuming the substring always exists and crashing on
NSNotFound. - Hard-coding positions that break later when the text changes.
Summary
- Use an attributed string when only part of the text should be bold.
- In UIKit,
NSMutableAttributedStringplus a bold.fontattribute is the standard solution. - Apply base styling first, then override the bold range.
- Compute ranges carefully and safely.
- Prefer the newer Swift
AttributedStringAPI when the platform and project allow it.

