UILabel
iOS
Swift
Text Truncation
Code Tutorial

How to check if UILabel is truncated?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UILabel does not expose a built-in isTruncated property, so truncation detection has to be inferred from layout. The usual technique is to compare the size the text would need with the size the label actually has after Auto Layout has finished. If the required height or width is larger than the label's bounds, the text is being clipped or truncated.

Compare Required Size With Actual Bounds

For most labels, the simplest check is to measure the text using the label's font and width constraints.

swift
1import UIKit
2
3extension UILabel {
4    func isTextTruncated() -> Bool {
5        guard let text = text, !text.isEmpty else {
6            return false
7        }
8
9        let maxSize = CGSize(width: bounds.width, height: .greatestFiniteMagnitude)
10        let requiredSize = text.boundingRect(
11            with: maxSize,
12            options: [.usesLineFragmentOrigin, .usesFontLeading],
13            attributes: [.font: font as Any],
14            context: nil
15        ).size
16
17        return ceil(requiredSize.height) > bounds.height
18    }
19}

This works well for multi-line labels when the label has already been laid out and bounds.width is meaningful.

Call It After Layout

Timing matters. If you check too early, the label may still have a zero width or an outdated frame.

A safe place to test is after layout has completed, such as in viewDidLayoutSubviews.

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3
4    if titleLabel.isTextTruncated() {
5        print("Label is truncated")
6    }
7}

This avoids a very common bug where the measurement is correct in theory but wrong in practice because Auto Layout has not finished yet.

Single-Line Labels Need Width Checks

If the label is configured for one line, width overflow is usually the relevant dimension.

swift
1extension UILabel {
2    func isSingleLineTruncated() -> Bool {
3        guard let text = text, !text.isEmpty else {
4            return false
5        }
6
7        let requiredWidth = (text as NSString).size(withAttributes: [.font: font as Any]).width
8        return requiredWidth > bounds.width
9    }
10}

This version is useful when numberOfLines is 1 and truncation appears as an ellipsis at the tail.

Attributed Text Requires Slightly More Care

If the label uses attributedText, measure the attributed string instead of plain text, because font, kerning, and paragraph style can change the result.

swift
1extension UILabel {
2    func isAttributedTextTruncated() -> Bool {
3        guard let attributedText = attributedText, attributedText.length > 0 else {
4            return false
5        }
6
7        let maxSize = CGSize(width: bounds.width, height: .greatestFiniteMagnitude)
8        let requiredSize = attributedText.boundingRect(
9            with: maxSize,
10            options: [.usesLineFragmentOrigin, .usesFontLeading],
11            context: nil
12        ).size
13
14        return ceil(requiredSize.height) > bounds.height
15    }
16}

That is the safer approach when the label contains mixed styles or custom paragraph settings.

When sizeThatFits Is Enough

sizeThatFits can also be a handy shortcut, especially for simple labels.

swift
let fitted = titleLabel.sizeThatFits(CGSize(width: titleLabel.bounds.width,
                                            height: .greatestFiniteMagnitude))
let truncated = fitted.height > titleLabel.bounds.height

This is often easier to read, though boundingRect gives you more direct control over the measurement behavior.

Common Pitfalls

The most common mistake is checking truncation before Auto Layout has set the label's final bounds. A measurement against width 0 tells you nothing useful.

Another issue is using plain text measurement when the label actually displays attributedText. The rendered result can be different.

Single-line and multi-line labels also need different thinking. Single-line truncation is mainly about width, while multi-line truncation is usually about height.

Finally, remember that numberOfLines = 0 allows expansion, so a label that is visually clipped may actually be constrained by an external layout container rather than by the label itself.

Summary

  • 'UILabel has no built-in truncation flag, so you must infer it from layout.'
  • Measure required text size and compare it with the label's actual bounds.
  • Perform the check after layout, not before.
  • Use width checks for single-line labels and height checks for multi-line labels.
  • Measure attributedText directly when styling affects the rendered size.

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.