How do I make UILabel display outlined text?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UILabel does not expose a built-in "outline" switch, so outlined text requires either custom drawing or an attributed string with stroke attributes. The right choice depends on whether you need a reusable label class or a quick one-off style for a specific label.
Use a UILabel Subclass for a Reusable Solution
If you want outlined text in multiple places, subclassing UILabel is the cleanest approach. You keep Auto Layout behavior, alignment, line breaking, and normal label APIs while controlling how the text is drawn.
The usual pattern is to draw the stroke first and the fill second:
This works because UIKit text drawing uses the current graphics context. By switching the drawing mode to .stroke, you get the border; by switching back to .fill, you get the normal interior.
Configure the Custom Label
You can use the subclass programmatically like any other label:
If you prefer Storyboards or XIBs, set the label's custom class to OutlinedLabel. If design-time control matters, expose the stroke settings to Interface Builder:
That makes it easier to experiment without recompiling for every small visual change.
Attributed Strings Are Fine for One-Off Labels
If you do not need a custom subclass, NSAttributedString can apply a fill and a stroke directly:
The negative stroke width is important. In UIKit drawing, a negative value means "draw fill and stroke." A positive value usually produces only the stroke.
This approach is simple, but it is less reusable when every outlined label in the app should behave consistently.
Layout and Rendering Considerations
Outlined text visually occupies a little more space than plain text, even when the label frame stays the same. Test a few things before shipping:
- Large font sizes.
- Multi-line labels.
- Dynamic Type sizes.
- Light and dark backgrounds.
If the outline touches the label edge, increase padding around the label or place it inside a container view. Thick strokes can make descenders and punctuation look cramped.
Performance is usually fine for a few labels, but custom drawing is still extra work. Avoid putting heavily outlined labels inside fast-scrolling lists unless you have actually tested it.
When a Different Effect Is Better
Sometimes people ask for outlined text when they really want a shadow or a layered title effect. A shadow is cheaper and often more readable:
That is not a true outline, but for many headers it solves the readability problem with less custom code.
Common Pitfalls
- Expecting a stock
UILabelproperty to enable a true text outline. - Drawing the stroke but forgetting to redraw the fill.
- Using a positive
strokeWidthwith attributed text and wondering why the fill disappeared. - Choosing a very thick outline that reduces readability instead of improving it.
- Forgetting to test long strings, large fonts, and multi-line layout.
Summary
- '
UILabeldoes not provide a built-in outlined text option.' - A custom subclass is the best reusable approach when the style appears in multiple places.
- '
NSAttributedStringwith stroke attributes is a good one-off solution.' - Negative
strokeWidthvalues are typically required for fill plus stroke rendering. - Test outlined text with real font sizes and layouts so the effect stays readable.
Related reading
- How do I make WRAP_CONTENT work on a RecyclerView
- How do I modify the background color of a List in SwiftUI?
- How do I obtain crash-data from my Android application?
- How do I open developer tools on iOS Simulator?
- How do I open phone settings when a button is clicked?
- How do I open phone settings when a button is clicked?
- How do I pass data between Activities in Android application?
- How do I pass data between Activities in Android application?
.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.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.