How to embed small icon in UILabel
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If you want a small icon inline with label text on iOS, the usual solution is an attributed string with an NSTextAttachment. That gives you much better control than trying to fake the effect with separate image and label views, especially when the icon must sit naturally inside the text flow.
Basic Inline Icon with NSTextAttachment
Create an attachment, give it an image, and insert it into a mutable attributed string before or after the text.
The bounds adjustment is important because an attachment often sits a little too low relative to the text baseline unless you nudge it.
Sizing the Icon to Match the Font
Hard-coding the icon size is fine for small cases, but you often get a better result by deriving the image size from the label font:
Using the font metrics keeps the icon visually aligned when the text size changes.
Tinting the Embedded Icon
If you use SF Symbols or template images, tint the attachment image before assigning it:
This is often cleaner than tinting the whole label, because the text and the icon may need different colors.
Multiple Icons or Mixed Formatting
Because the content is an attributed string, you can combine icons, different fonts, and colored text in one label:
This is one of the main reasons inline attachments are better than arranging a tiny UIImageView next to a UILabel when the content behaves like a single typographic unit.
When Not to Use an Attachment
If the icon must respond to taps independently, animate separately, or stay perfectly aligned in complex multiline layouts, a combined view hierarchy may still be the better choice. NSTextAttachment is best when the icon is decorative or semantically part of the text.
For example, a badge icon inside a sentence works well as an attachment. A clickable trailing action icon usually does not.
Common Pitfalls
The biggest mistake is forgetting to set attachment.bounds. Without it, the image often appears too large or vertically misaligned relative to the surrounding text.
Another issue is using a full-color image without checking rendering mode. Template images and tinted SF Symbols behave differently from pre-rendered assets, so icon color problems often come from the image configuration rather than from the label.
Finally, test multiline labels carefully. Attachments participate in text layout, but large icons or inconsistent baseline offsets can make line height and wrapping look awkward.
Summary
- Use
NSTextAttachmentinside an attributed string for inline icons inUILabel. - Adjust
attachment.boundsto control size and baseline alignment. - Match the icon size to the label font for a cleaner result.
- Tint template images or SF Symbols before attaching them when needed.
- Prefer a separate image view only when the icon needs its own behavior or interaction.
Related reading
- How to encode dependency path as a feature for classification?
- How to fetch vectors for a word list with Word2Vec?
- How to find all permutations of a given word in a given text?
- How to find and replace all occurrences of a substring in a string?
- How to emulate GPS location in the Android Emulator?
- How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?
- How to find num_words or vocabulary size of Keras tokenizer when one is not assigned?
- How to fix RuntimeError Missing implementation that supports loader when calling hub.text_embedding_column method?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.