iOS
NSString
bold text
text formatting
Swift programming

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:

objc
NSString *text = @"Hello world";

If you want "world" to appear bold while "Hello " stays regular, you need NSAttributedString or NSMutableAttributedString.

Objective-C example:

objc
1#import <UIKit/UIKit.h>
2
3NSString *text = @"Hello world";
4NSMutableAttributedString *attr =
5    [[NSMutableAttributedString alloc] initWithString:text];
6
7[attr addAttribute:NSFontAttributeName
8             value:[UIFont boldSystemFontOfSize:17.0]
9             range:[text rangeOfString:@"world"]];

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.

objc
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 200, 40)];
label.attributedText = attr;

That is the core pattern:

  1. create the plain text
  2. build a mutable attributed string
  3. apply a bold font to a range
  4. 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.

objc
1UIFont *regular = [UIFont systemFontOfSize:17.0];
2UIFont *bold = [UIFont boldSystemFontOfSize:17.0];
3
4NSMutableAttributedString *attr =
5    [[NSMutableAttributedString alloc] initWithString:text
6                                           attributes:@{
7                                               NSFontAttributeName: regular
8                                           }];
9
10[attr addAttribute:NSFontAttributeName
11             value:bold
12             range:[text rangeOfString:@"world"]];

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:

swift
1import UIKit
2
3let text = "Hello world"
4let attributed = NSMutableAttributedString(
5    string: text,
6    attributes: [
7        .font: UIFont.systemFont(ofSize: 17)
8    ]
9)
10
11if let range = text.range(of: "world") {
12    let nsRange = NSRange(range, in: text)
13    attributed.addAttributes([
14        .font: UIFont.boldSystemFont(ofSize: 17)
15    ], range: nsRange)
16}
17
18let label = UILabel()
19label.attributedText = attributed

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.

swift
1let text = "Name: Ada, Role: Engineer"
2let attributed = NSMutableAttributedString(string: text)
3
4let boldParts = ["Ada", "Engineer"]
5
6for part in boldParts {
7    if let range = text.range(of: part) {
8        attributed.addAttributes([
9            .font: UIFont.boldSystemFont(ofSize: 16)
10        ], range: NSRange(range, in: text))
11    }
12}

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:

swift
1let prefix = "Total: "
2let amount = "$42.00"
3let full = prefix + amount
4
5let attributed = NSMutableAttributedString(string: full)
6attributed.addAttributes([
7    .font: UIFont.boldSystemFont(ofSize: 17)
8], range: NSRange(location: prefix.count, length: amount.count))

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 NSString cannot store bold styling by itself.
  • Use NSMutableAttributedString to apply font attributes to a substring range.
  • Assign the result to attributedText on a UIKit control.
  • Prefer explicit regular and bold fonts for consistent rendering.
  • Avoid fragile hardcoded ranges when the content is dynamic or localized.

Course illustration
Course illustration

All Rights Reserved.