How to Draw a Border Around a UILabel in Swift and UIKit

How to Draw a Border Around a UILabel in Swift and UIKit

Written by

in

Quick answer: A UILabel draws its border through its backing CALayer, not through a label-specific property. Set label.layer.borderWidth to a non-zero value and label.layer.borderColor to a CGColor, then add layer.cornerRadius for rounded corners. Setting only borderColor does nothing, because borderWidth defaults to 0.

Why this happens

UILabel inherits from UIView, and every UIView is backed by a CALayer. The label itself has no border, borderColor or cornerRadius property — those live on the layer. When you ask a label to draw a border, you are really asking Core Animation to stroke the edge of its backing layer.

That is also why the border is drawn inside the layer’s bounds. It never expands the label’s frame, never changes its intrinsic content size, and never pushes neighbouring views around. For debugging text placement this is exactly what you want: the border shows you the exact rectangle the label occupies without disturbing the layout you are inspecting.

Two defaults trip people up. borderWidth starts at 0, so assigning a borderColor alone produces nothing visible. And borderColor is a CGColor, not a UIColor, so you must convert it — and a CGColor is a snapshot, which matters later for dark mode.

How to fix it

1. Set borderWidth and borderColor on the layer

This is the minimal fix. In Swift you do not need to import QuartzCore; UIKit already brings the layer properties into scope.

import UIKit

final class ViewController: UIViewController {

    @IBOutlet weak var debugLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        debugLabel.layer.borderWidth = 1.0
        debugLabel.layer.borderColor = UIColor.systemGreen.cgColor
    }
}

If you are working in Objective-C, the equivalent needs the QuartzCore header for CALayer:

#import <QuartzCore/QuartzCore.h>

self.debugLabel.layer.borderColor = [UIColor greenColor].CGColor;
self.debugLabel.layer.borderWidth = 3.0;

A borderWidth of 1.0 is one point, which is two or three physical pixels on a Retina screen. For a true hairline, divide by the display scale:

debugLabel.layer.borderWidth = 1.0 / traitCollection.displayScale

2. Round the corners at the same time

cornerRadius rounds the layer’s background and its border, so the two settings compose naturally. You do not need masksToBounds just to get a rounded border — that flag only controls whether sublayers and content are clipped to the rounded shape.

label.layer.borderWidth = 2.0
label.layer.borderColor = UIColor.systemBlue.cgColor
label.layer.cornerRadius = 8

You do need masksToBounds (or clipsToBounds) when the label has a background colour and you want the square corners of that background hidden:

label.backgroundColor = .systemYellow
label.layer.cornerRadius = 8
label.layer.masksToBounds = true
label.layer.borderWidth = 2.0
label.layer.borderColor = UIColor.systemOrange.cgColor

Without masksToBounds = true the background colour still fills the full rectangle behind the rounded border, which looks like a square label with a rounded outline drawn on top.

3. Make it reusable with an @IBInspectable subclass

Interface Builder’s attributes inspector has no border section for UILabel, so if you want to set this visually, subclass the label and expose the values.

import UIKit

@IBDesignable
final class BorderedLabel: UILabel {

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet { layer.borderWidth = borderWidth }
    }

    @IBInspectable var borderColor: UIColor = .clear {
        didSet { applyBorderColor() }
    }

    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet { layer.cornerRadius = cornerRadius }
    }

    override func traitCollectionDidChange(_ previous: UITraitCollection?) {
        super.traitCollectionDidChange(previous)
        applyBorderColor()
    }

    private func applyBorderColor() {
        layer.borderColor = borderColor.resolvedColor(with: traitCollection).cgColor
    }
}

Set the label’s class to BorderedLabel in the identity inspector and the three properties appear in the attributes inspector.

4. Use a background colour when you only need to debug

If the goal is purely to see where the label sits, a translucent background is often faster than a border and shows the text baseline more clearly:

label.backgroundColor = UIColor.systemRed.withAlphaComponent(0.2)

Xcode’s Debug View Hierarchy and the Simulator’s Color Blended Layers option give you the same information without touching the code at all.

5. The SwiftUI equivalent

If you have moved the view to SwiftUI, there is no layer to reach into. Use an overlay stroke, which is the closest match to a rounded border:

import SwiftUI

Text("Debug")
    .padding(8)
    .overlay(
        RoundedRectangle(cornerRadius: 8)
            .stroke(Color.gray, lineWidth: 1)
    )

.border(Color.gray) also exists, but it draws a plain rectangle with square corners and no radius control.

How to prevent it

  • Always set borderWidth before or alongside borderColor; a zero width makes the colour invisible.
  • Remember borderColor takes a CGColor — use UIColor.someColor.cgColor, not the UIColor itself.
  • Do not rely on a CGColor created from a dynamic UIColor to update automatically. Re-resolve it when the trait collection changes, as in the subclass above.
  • Use masksToBounds = true whenever a background colour and a corner radius are combined.
  • Do not expect the border to add padding. If the text touches the border, add insets with a UILabel subclass overriding textRect(forBounds:limitedToNumberOfLines:) and drawText(in:), or wrap the label in a container view.
  • Keep debug borders out of shipping builds, or gate them behind #if DEBUG.

FAQ

Why does my label show no border after setting borderColor?

Because layer.borderWidth defaults to 0. A colour with no width draws nothing. Set borderWidth to at least 1.0 (or a hairline value) and the border appears.

Does adding a border change the label’s size or layout?

The border is painted inside the layer’s existing bounds, so the frame and intrinsic content size stay the same. If you need breathing room between the text and the border, add padding through a subclass or a container view rather than increasing borderWidth.

Why does the border colour look wrong in dark mode?

layer.borderColor is a CGColor, which has no notion of light or dark appearance. A colour resolved once at launch keeps that value. Re-assign the border colour when the trait collection changes, or use UIColor.resolvedColor(with:) with the current trait collection each time you set it.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.