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
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.
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.
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.textinstead oflabel.attributedText, which discards the formatting. - Hard-coding ranges incorrectly so the wrong characters become bold.
- Forgetting that
NSAttributedStringusesNSRange, not SwiftRange<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
NSMutableAttributedStringand assign it toUILabel.attributedText. - Apply bold and regular fonts to different
NSRangevalues. - 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.

