UILabel
iOS development
Swift programming
UIKit
border drawing

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.

Browse interview questions

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:

swift
1import UIKit
2
3let label = UILabel()
4label.text = "Status"
5label.textAlignment = .center
6
7label.layer.borderWidth = 1
8label.layer.borderColor = UIColor.systemBlue.cgColor
9label.layer.cornerRadius = 8
10label.layer.masksToBounds = true

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.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    @IBOutlet private weak var statusLabel: UILabel!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        statusLabel.text = "Waiting"
10        statusLabel.textAlignment = .center
11        statusLabel.layer.borderWidth = 2
12        statusLabel.layer.borderColor = UIColor.systemGray.cgColor
13        statusLabel.layer.cornerRadius = 6
14        statusLabel.layer.masksToBounds = true
15    }
16}

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.

swift
1import UIKit
2
3final class PaddedLabel: UILabel {
4    var textInsets = UIEdgeInsets(top: 6, left: 10, bottom: 6, right: 10)
5
6    override func drawText(in rect: CGRect) {
7        super.drawText(in: rect.inset(by: textInsets))
8    }
9
10    override var intrinsicContentSize: CGSize {
11        let size = super.intrinsicContentSize
12        return CGSize(
13            width: size.width + textInsets.left + textInsets.right,
14            height: size.height + textInsets.top + textInsets.bottom
15        )
16    }
17}

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 borderColor without borderWidth, which leaves the border invisible.
  • Forgetting that borderColor expects a CGColor, not a UIColor.
  • Using cornerRadius without masksToBounds and 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 UILabel border by styling its backing layer.
  • Set borderWidth and borderColor first, then add cornerRadius if needed.
  • Use masksToBounds when 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 CAShapeLayer only when the border style is more complex than a normal solid outline.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.