How to draw border around a UILabel?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UILabel inherits from UIView, which means you can style its backing layer directly to draw a border. The usual approach is to set the layer's borderWidth, borderColor, and optionally cornerRadius so the label border matches the rest of the interface.
Use the Label's Layer
The simplest border comes from the view's CALayer:
That is enough for a clean solid border. The masksToBounds line matters when you use rounded corners, because it keeps the visual shape consistent.
Configure the Border in a View Controller
In real code, you usually style the label after it has been created or connected from a storyboard outlet.
This approach works well for standard rectangular or rounded-rectangle label borders.
Add Padding So the Border Has Space
A border can look cramped if the text sits too close to the edges. UILabel does not provide built-in content insets, so if you want padding as well as a border, a small subclass is often cleaner.
With that subclass, the border surrounds the text more naturally instead of hugging it too tightly.
Use CAShapeLayer for Special Borders
For simple solid borders, the label's main layer is enough. If you need dashed or custom-shaped borders, use CAShapeLayer instead.
That is useful when you want:
- dashed borders
- animated border strokes
- custom rounded paths beyond the default layer border
For most apps, though, direct layer styling is the simplest and most maintainable solution.
Interface Builder Works Too
If the label comes from a storyboard or XIB, you can still apply the same layer settings in viewDidLoad() or in a small helper method. The important part is that a border on UILabel is still just view-layer styling, no matter whether the label was created in code or visually in Interface Builder.
That also makes it easy to keep several labels visually consistent across a screen, because the same helper can apply color, width, and corner radius in one place instead of repeating small styling fragments everywhere.
If your design system uses theme colors, it is also worth centralizing the styling in a helper or subclass so border updates happen in one location. That keeps Interface Builder screens and programmatic views visually aligned instead of drifting over time.
Common Pitfalls
- Setting only
borderColorwithoutborderWidth, which leaves the border invisible. - Forgetting that
borderColorexpects aCGColor, not aUIColor. - Using
cornerRadiuswithoutmasksToBoundsand then wondering why the border or content looks wrong. - Adding a border but forgetting text padding, which can make the label feel cramped.
- Rebuilding custom border layers repeatedly during layout when a simple layer border would have been enough.
Summary
- Draw a
UILabelborder by styling its backing layer. - Set
borderWidthandborderColorfirst, then addcornerRadiusif needed. - Use
masksToBoundswhen the label should actually clip to its rounded shape. - Add padding with a small subclass if the border needs breathing room around the text.
- Reach for
CAShapeLayeronly when the border style is more complex than a normal solid outline.
Related reading
- How to draw border on just one side of a linear layout?
- How to easily resize/optimize an image size with iOS?
- How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar iOS 7
- How to embed small icon in UILabel
- How to emulate GPS location in the Android Emulator?
- How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?
- How to Enable Internal App Sharing for Android?
- How to enable zoom controls and pinch zoom in a WebView?
.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.