Mobile Development 17 min read

Understanding UILabel’s Internal Text Management and Common Pitfalls

This article examines how UILabel stores its text‑related properties internally, reveals the role of the private _content object, and explains the interactions and pitfalls when using the text and attributedText properties in iOS development.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding UILabel’s Internal Text Management and Common Pitfalls

Introduction

UILabel is a frequently used UI control in iOS development. This article explores how Apple’s underlying implementation manages UILabel’s text information and the common pitfalls developers may encounter.

Basic Properties

Code snippet showing creation of UILabel and logging of its default properties such as textColor, font, shadowColor, and shadowOffset.

UILabel *label = [[UILabel alloc] init];
NSLog(@"%@", label.textColor);
NSLog(@"%@", label.font);
NSLog(@"%@", label.shadowColor);
NSLog(@"%@", @(label.shadowOffset));
// ...

Inspection of the internal ivars reveals that the visible properties are not stored directly in separate ivars but inside a private _content member of type UILabelContent .

content (UILabelContent*): <_UILabelContent: 0x60000260cac0>

Hypothesis 1

Assume the properties are stored in corresponding member variables.

Printing the ivar list shows no explicit textColor , font , _shadowColor , _shadowOffset variables, suggesting they are stored elsewhere.

Hypothesis 2

Identify the _content ivar as the storage location.

in UILabel:
    _content (_UILabelContent*): <_UILabelContent: 0x60000260cac0>
    ...

Using KVC to print _content confirms that textColor, font, paragraph style, and shadow are encapsulated inside this object.

text Property

When text is assigned, the underlying _content changes from _UILabelContent to _UILabelStringContent , which stores the raw string, its length, and an attribute dictionary.

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"ByteDance";
}
(lldb) po [label valueForKey:@"content"]
<_UILabelStringContent:0x600003573780 len=9 ByteDance attr={...}>

attributedText Property

Assigning an NSAttributedString without explicit attributes creates a _UILabelAttributedStringContent that contains a default rich‑text representation.

UILabel *label = [[UILabel alloc] init];
NSString *text = @"ByteDance";
label.attributedText = [[NSAttributedString alloc] initWithString:text];

The internal _content shows default color, font, paragraph style, and a shadow.

Problem 1

When no attributes are added, UILabel adds default attributes during rendering.

Problem 2

Adding attributes to a range overrides only that range; the rest of the text uses the label’s default attributes.

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
label.attributedText = attStr;

Result: the first character appears red, the remaining characters use the label’s default color.

Problem 3

Applying attributes to the entire string replaces the label’s default attribute dictionary, so label.textColor reflects the new value.

[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attStr.length)];
label.attributedText = attStr;

Interaction Between text and attributedText

If attributedText is set first and later text is assigned, the label displays the plain string and discards the previously set attributed attributes (Option 1). Clearing attributedText before setting text restores the default attribute dictionary.

Conclusion

When using both text and attributedText on a UILabel, clear attributedText before assigning text to avoid unintentionally changing the label’s default color or other attributes.

Mobile DevelopmentiOSUIKitTEXTattributedTextUILabel
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.