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.
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.
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.
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.
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.
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.
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
- '
UILabelhas 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
attributedTextdirectly when styling affects the rendered size.
Related reading
- How to check internet access on Android? InetAddress never times out
- How to check internet access on Android? InetAddress never times out
- How to check internet connection in alamofire?
- How to check internet connection in React Native application for both iOS and Android?
- How to check iOS version?
- How to check iOS version?
- How To Check Response.statusCode in sendSynchronousRequest on Swift
- How to check version of a CocoaPods framework
.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.