UILabel
text length
width calculation
iOS development
Swift programming

How to calculate UILabel width based on text length?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Calculating a UILabel width is not about character count alone. The real width depends on the font, font size, line break behavior, and whether the label is single-line or multiline. In UIKit, the usual approach is to measure the rendered text with the same font attributes the label will actually use.

Measure Single-Line Text with size(withAttributes:)

For a simple one-line label, NSString.size(withAttributes:) is often enough.

swift
1import UIKit
2
3let text = "Hello, UILabel"
4let font = UIFont.systemFont(ofSize: 17)
5
6let size = (text as NSString).size(withAttributes: [.font: font])
7print(size.width)

This gives the natural rendered width for that exact string and font. It is useful when you are laying out views programmatically and need a width estimate before creating constraints.

Use boundingRect for More Realistic Measurement

If line wrapping, long text, or constrained height matters, boundingRect is more accurate.

swift
1import UIKit
2
3func labelWidth(for text: String, font: UIFont, height: CGFloat) -> CGFloat {
4    let rect = (text as NSString).boundingRect(
5        with: CGSize(width: .greatestFiniteMagnitude, height: height),
6        options: [.usesLineFragmentOrigin, .usesFontLeading],
7        attributes: [.font: font],
8        context: nil
9    )
10    return ceil(rect.width)
11}
12
13let width = labelWidth(for: "Dynamic text", font: .systemFont(ofSize: 17), height: 24)
14print(width)

Using ceil helps avoid fractional pixel clipping.

Prefer Auto Layout When You Do Not Need Manual Widths

In many real interfaces, you do not actually need to calculate width manually. If the label is inside a stack view or constraint-driven layout, Auto Layout can size it from intrinsic content size.

Manual measurement makes sense when:

  • you are computing a custom frame
  • you need to size a container before applying constraints
  • you are building a custom layout or collection view sizing rule

If you only want the label to fit its text, sizeToFit() may be enough.

swift
1let label = UILabel()
2label.text = "Short text"
3label.font = .systemFont(ofSize: 17)
4label.sizeToFit()
5print(label.frame.width)

Multiline Labels Need a Different Mental Model

For multiline labels, width and height interact. If the width is fixed, measure height. If the height is fixed, measure width. But you usually do not measure a multiline label by plain text length because wrapping changes the result dramatically.

That is why correct measurement must use the same font, maximum width, and line settings that the real label will use.

Insets and Custom Padding Change the Width

If your label lives inside a custom subclass that adds text insets or padding, raw text measurement is no longer enough by itself. You need to add the horizontal inset values to the measured text width, otherwise the layout will still clip or misalign even when the font measurement is correct.

Font Changes Mean Width Changes

If the label uses Dynamic Type or any runtime font change, cached width values can become stale immediately. Recalculate when the font changes or when the content size category updates. Otherwise the layout may look correct in testing but clip text on devices with larger accessibility text sizes.

Common Pitfalls

  • Estimating label width from character count instead of actual font metrics.
  • Measuring with one font and rendering with another.
  • Forgetting to round up with ceil, which can cause clipping.
  • Manually measuring labels when Auto Layout or sizeToFit() would be simpler.
  • Treating multiline text as a simple one-line width problem.

Summary

  • 'UILabel width depends on rendered font metrics, not just text length.'
  • Use size(withAttributes:) for quick single-line measurement.
  • Use boundingRect for more realistic sizing scenarios.
  • Let Auto Layout handle sizing when possible.
  • For multiline text, measure with the same constraints and line behavior the label will actually use.

Course illustration
Course illustration

All Rights Reserved.