UITextView style is being reset after setting text property
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If a UITextView loses formatting after you assign to text, that is expected UIKit behavior. The text property represents plain text, so setting it replaces the current contents without preserving attributes such as fonts, colors, paragraph styles, or links.
When styling matters, use attributedText or edit the text view's textStorage. The correct fix depends on whether you want a single uniform style or rich formatting on specific ranges.
Why Setting text Resets the Style
A UITextView has two related but different models:
- '
textfor plain text' - '
attributedTextfor rich text with attributes attached to ranges'
When you write:
UIKit builds new plain content. It does not know how to preserve prior attribute runs because the new value is just a String.
That is why partial styling disappears. The old attributed content has been replaced.
Use attributedText for Styled Content
If the content needs formatting, build an attributed string and assign that instead.
This is the direct replacement for code that wrongly relied on text while expecting style to survive.
Reapply a Uniform Base Style
Sometimes you do not need complex rich text. You just want the whole text view to keep one font and one color whenever the string changes. In that case, rebuild attributedText with a base attribute set.
This works well when the view has one consistent style rather than many styled ranges.
Edit textStorage for Rich-Text Updates
If only part of the content changes, editing textStorage is often better than rebuilding the entire attributed string manually.
That approach is useful when only certain words need styling changes.
Do Not Forget typingAttributes
If the text view is editable, new user input uses typingAttributes, not automatically whatever you last assigned elsewhere.
Without this, the view may look correct after programmatic replacement but switch to an unexpected style when the user starts typing.
Preserve Selection in Editable Views
Replacing the entire contents can move the cursor or clear the selection. If the text view is interactive, save and restore selectedRange around the update.
That small detail matters in editors, forms, and chat inputs where cursor jumps feel broken.
Common Pitfalls
The main mistake is setting attributedText and then later overwriting it with text, which removes the styling again. Another is assuming font and textColor are enough to restore rich-text ranges; they only define a default appearance. Developers also forget typingAttributes in editable text views, so user-entered characters use the wrong style. Finally, replacing the whole text without restoring selection can make the control feel glitchy even if the visual formatting is correct.
Summary
- Setting
textresets styling because it replaces rich text with plain text. - Use
attributedTextwhen formatting must survive updates. - For uniform styling, rebuild an attributed string with a base attribute set.
- For partial rich-text edits, work through
textStorage. - In editable views, also manage
typingAttributesand selection state.

