Any way to bold part of a NSString?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You cannot make part of an NSString bold by changing the NSString itself, because NSString stores characters, not visual styling. To bold only part of the text, you need an attributed string and a UI component such as UILabel, UITextView, or UIButton that knows how to render those attributes.
Use NSAttributedString, Not Plain NSString
A plain string has content only:
If you want "world" to appear bold while "Hello " stays regular, you need NSAttributedString or NSMutableAttributedString.
Objective-C example:
At this point the attributed string contains both the text and the styling instructions for the selected range.
Apply It to a Label
The styled text becomes visible only when assigned to a control that supports attributed text.
That is the core pattern:
- create the plain text
- build a mutable attributed string
- apply a bold font to a range
- assign it to the UI element
Use Full Font Attributes, Not Only Traits Guessing
When bolding text, do not assume the control will infer the correct font size automatically. It is safer to define both the regular and bold fonts explicitly.
This ensures the entire string has a consistent base font and only the selected portion differs in weight.
Swift Version
If your project is in Swift, the same idea is simpler to read:
The main extra step in Swift is converting the String range into an NSRange because attributed strings use Foundation range types.
Bolding Multiple Parts
If you need to bold several substrings, apply the attribute more than once.
This pattern is common in profile cards, summaries, and settings screens.
Dynamic Strings Need Safe Range Handling
If the bolded text comes from user content or localization, do not hardcode numeric ranges unless you fully control the string format. Search for the substring or build the string from parts so the styled range stays correct across languages and content changes.
A safer construction pattern is:
Because the string was assembled deliberately, the range is less fragile than one copied from a static mockup.
Common Pitfalls
The most common mistake is trying to style an NSString directly. Styling belongs to attributed strings and rendering views, not to plain text storage.
Another issue is using the wrong range after localization or string changes. Hardcoded offsets break easily once the text stops being static.
A third problem is assigning the styled string to a property such as text instead of attributedText. That silently discards the formatting.
Summary
- A plain
NSStringcannot store bold styling by itself. - Use
NSMutableAttributedStringto apply font attributes to a substring range. - Assign the result to
attributedTexton a UIKit control. - Prefer explicit regular and bold fonts for consistent rendering.
- Avoid fragile hardcoded ranges when the content is dynamic or localized.

