NSAttributedString background color and rounded corners
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NSAttributedString can apply a background color to text, but it cannot directly express rounded corners as a built-in text attribute. That distinction matters because many developers expect the background attribute to behave like a rounded tag or pill. If you need rounded corners, you usually have to custom-draw the background or use a different UI composition strategy.
What the Background Attribute Actually Does
The .backgroundColor text attribute paints a rectangular background behind glyph runs. It does not know anything about corner radius.
This works for simple highlighting, but the background is effectively square. There is no standard attributed-string key such as "cornerRadius" that text rendering engines honor automatically.
Use a Custom View When You Need a Rounded Tag
If the goal is a pill-shaped label or badge, the simplest solution is often not NSAttributedString at all. A padded UILabel or custom view is easier to maintain.
If the entire label needs one rounded background, this approach is usually better than forcing the problem into text attributes.
Use Text Layout APIs for Partial Rounded Highlights
If you must round the background behind only part of an attributed string, you need custom drawing based on the text layout. One approach is to use NSLayoutManager, find the glyph bounding rect for the target range, and draw a rounded rectangle before drawing the text.
That is more advanced, but it matches the actual rendering model: first compute layout, then draw the custom background shape, then draw the attributed text on top.
A Practical Drawing Pattern
The basic flow is:
- build the attributed string
- compute the rect for the highlighted range
- draw a
UIBezierPathrounded rect in that area - draw the text
The exact implementation depends on whether you are drawing in UILabel, UITextView, or a custom Core Text or TextKit view. The important point is that rounded corners come from your drawing code, not from a built-in string attribute.
When to Avoid Custom Text Drawing
Custom drawing is powerful, but it increases complexity around multiline layout, dynamic type, selection, and accessibility. If the visual requirement is really a chip, badge, or token, separate views are often better than partial attributed text styling.
That tradeoff matters most when the highlighted text wraps across lines. A nice single-line rounded shape can become a much harder layout problem once the text spans multiple fragments.
Common Pitfalls
- Expecting
.backgroundColoronNSAttributedStringto support rounded corners directly. - Building a complex custom text renderer when a rounded
UILabelwould solve the actual UI need. - Forgetting that partial highlights may span multiple line fragments.
- Trying to solve a view-layer problem only with attributed-string keys.
- Overlooking accessibility and dynamic type when custom-drawing text backgrounds.
Summary
- '
NSAttributedStringsupports background color, but not rounded-corner background styling as a native attribute.' - For full-label rounded backgrounds, a padded
UILabelor custom view is usually simpler. - For rounded highlights on part of a string, use custom drawing based on text layout.
- Multiline and accessibility concerns make custom rendering more complex than it first appears.
- Choose between text attributes and separate UI views based on the real visual requirement.

