Why this happens
UILabel displays plain text by default through its text property, which applies a single font style to the entire string. To show mixed styling—such as bold and regular text in the same label—you must switch to attributedText, which accepts an NSAttributedString. This object stores character-level attributes like font, color, and paragraph style, letting you apply different formatting to different ranges of the same string.
The key classes are NSMutableAttributedString for building the string and NSAttributedString.Key for specifying attributes such as .font and .foregroundColor. You define a base style for the whole string, then override specific ranges with alternate styles.
How to fix it
1. Build an attributed string with a bold default and a regular range (Swift 5+)
This approach creates a reusable function that takes a plain string and an optional NSRange for the non-bold portion. It returns a ready-to-use NSAttributedString.
import UIKit
func attributedString(from string: String, nonBoldRange: NSRange?) -> NSAttributedString {
let fontSize = UIFont.systemFontSize
let boldAttrs: [NSAttributedString.Key: Any] = [
.font: UIFont.boldSystemFont(ofSize: fontSize),
.foregroundColor: UIColor.label
]
let regularAttrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: fontSize),
.foregroundColor: UIColor.label
]
let attrStr = NSMutableAttributedString(string: string, attributes: boldAttrs)
if let range = nonBoldRange {
attrStr.setAttributes(regularAttrs, range: range)
}
return attrStr
}
Usage:
let targetString = "Updated 2024/11/22 14:30"
// Make the date portion regular (starting at index 8, length 10)
let range = NSRange(location: 8, length: 10)
let label = UILabel()
label.attributedText = attributedString(from: targetString, nonBoldRange: range)
label.sizeToFit()
The function uses UIFont.systemFontSize so it respects the system text size. UIColor.label adapts automatically to light and dark mode.
2. Handle dynamic strings and localization safely
Hard-coded ranges break when the string changes length or when localized. Compute the range at runtime by searching for the substring you want to style.
func attributedStringWithLocalizedDate(_ date: Date) -> NSAttributedString {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
let fullDateString = formatter.string(from: date)
// Build the full display string using a localized format
let format = NSLocalizedString("Updated %@", comment: "Timestamp format")
let targetString = String(format: format, fullDateString)
// Find the date-only portion (without time) to keep regular
formatter.timeStyle = .none
let dateOnlyString = formatter.string(from: date)
guard let range = targetString.range(of: dateOnlyString) else {
// Fallback: everything bold
let attrs: [NSAttributedString.Key: Any] = [
.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize),
.foregroundColor: UIColor.label
]
return NSAttributedString(string: targetString, attributes: attrs)
}
// Convert Swift Range<String.Index> to NSRange
let nsRange = NSRange(range, in: targetString)
return attributedString(from: targetString, nonBoldRange: nsRange)
}
Call it like:
label.attributedText = attributedStringWithLocalizedDate(Date())
This works across languages because the range is derived from the formatted output, not hard-coded indices.
3. Quick one-liner for simple cases
When you only need to bold a known substring once, skip the helper function:
let string = "Price: $19.99"
let attributed = NSMutableAttributedString(string: string,
attributes: [.font: UIFont.boldSystemFont(ofSize: 17),
.foregroundColor: UIColor.label])
if let range = string.range(of: "$19.99") {
let nsRange = NSRange(range, in: string)
attributed.setAttributes([.font: UIFont.systemFont(ofSize: 17)],
range: nsRange)
}
label.attributedText = attributed
4. Objective-C category for legacy codebases
If you maintain Objective-C, a category on UILabel keeps call sites clean:
// UILabel+BoldSubstring.h
@interface UILabel (BoldSubstring)
- (void)boldSubstring:(NSString *)substring;
- (void)boldRange:(NSRange)range;
@end
// UILabel+BoldSubstring.m
@implementation UILabel (BoldSubstring)
- (void)boldRange:(NSRange)range {
if (![self respondsToSelector:@selector(setAttributedText:)]) return;
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]
initWithAttributedString:self.attributedText ?: [[NSAttributedString alloc] initWithString:self.text]];
[attr setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:self.font.pointSize]}
range:range];
self.attributedText = attr;
}
- (void)boldSubstring:(NSString *)substring {
NSRange range = [self.text rangeOfString:substring];
if (range.location != NSNotFound) [self boldRange:range];
}
@end
Usage:
self.label.text = @"Updated: 2024/11/22 14:30";
[self.label boldSubstring:@"2024/11/22"]; // date stays regular
How to prevent it
- Always use
attributedTextinstead oftextwhen any mixed styling is needed. - Compute ranges dynamically from the final formatted string; never hard-code indices.
- Use
NSLocalizedStringwith format specifiers (%@) so localized strings keep the same placeholder positions. - Test with longer languages (German, Russian) and right-to-left scripts (Arabic, Hebrew) to verify ranges still map correctly.
- Prefer
UIFontMetricsfor scaling if you support Dynamic Type beyond the system font size.
FAQ
Can I apply more than two styles in the same label?
Yes. Call setAttributes(_:range:) multiple times with different ranges and attribute dictionaries. Each call overrides the attributes for its range.
Does this work with SwiftUI’s Text view?
SwiftUI uses a different system. Combine Text views with + or use AttributedString (iOS 15+) with Markdown-style syntax: Text("**Bold** and regular").
Why does my label show the wrong style after rotating or changing trait collections?
If you cache the attributed string, recreate it in traitCollectionDidChange(_:) or override traitCollectionDidChange(_:) to rebuild with the current UIColor.label and font metrics.
Related resources
- Apple Documentation: NSAttributedString
- Apple Documentation: UILabel attributedText
- Swift String and Range Conversion Guide
Need to style text in SwiftUI instead? See our guide on AttributedString in SwiftUI.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.