Replacement for deprecated -sizeWithFontconstrainedToSizelineBreakMode in iOS 7?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When -sizeWithFont:constrainedToSize:lineBreakMode: was deprecated in iOS 7, the practical replacement for multi-line text measurement became boundingRectWithSize:options:attributes:context:. For simple single-line measurement, sizeWithAttributes: is often enough. The real change was not just API naming. Apple moved text measurement toward attribute dictionaries and more flexible drawing options.
The Modern Replacement for Multi-Line Text
If you previously measured wrapped text inside a constrained box, use boundingRectWithSize:options:attributes:context:.
Objective-C example:
This is the usual replacement because it supports:
- constrained widths
- multi-line wrapping
- attributed text measurement
- paragraph and font-related options
The call returns a CGRect, but in most sizing scenarios you only care about the size.
Why sizeWithAttributes: Is Not the Full Replacement
You may also see sizeWithAttributes::
This is useful for straightforward single-line measurement, but it does not replace the old constrained multi-line API in the general case.
A practical rule is:
- single-line text with no wrapping:
sizeWithAttributes: - wrapped or constrained text:
boundingRectWithSize:options:attributes:context:
That distinction avoids a lot of confusion.
Measuring Text for a UILabel
A common use case is calculating height for a label with fixed width:
If you later assign that text to a UILabel, make sure the label itself is configured consistently:
- same font
- same width constraint
- '
numberOfLines = 0for multi-line content' - compatible line break mode
If the label configuration and the measurement configuration differ, the calculated size will be wrong even if the API call is correct.
Paragraph Style Matters
If your label uses a non-default line break mode or paragraph style, include that in the attributes. Otherwise you are not measuring the same text layout the user will see.
Example:
This is especially important if your app relies on truncation, custom line spacing, or attributed strings.
Why ceil Is Usually Necessary
Text measurement often returns fractional sizes. If you assign those directly to frames, the text can clip or render slightly off.
That is why many sizing examples use:
Rounding up ensures there is enough space for the rendered glyphs.
Attributed Strings Fit Naturally
The newer API design also fits attributed strings better than the old method did. If your text includes multiple fonts, kerning, or paragraph settings, attribute-based measurement is the right model.
That is one reason the deprecated API was replaced instead of simply renamed. The newer call is more flexible and more aligned with the text rendering system Apple wanted developers to use.
Common Pitfalls
The biggest mistake is using sizeWithAttributes: for text that actually wraps across multiple lines. That often underestimates height because there is no constrained layout calculation.
Another issue is forgetting the NSStringDrawingUsesLineFragmentOrigin option. Without it, multi-line measurement is often not what you expect.
Developers also sometimes measure with one font and render with another, or measure without paragraph style while the actual label uses custom wrapping behavior. That mismatch causes incorrect sizes even when the API call looks right.
Finally, do not ignore fractional results. Use ceil on the measured dimensions to avoid clipping.
Summary
- The general replacement for the deprecated constrained text-sizing API is
boundingRectWithSize:options:attributes:context:. - Use
sizeWithAttributes:only for simpler single-line measurement. - Include font and paragraph style attributes that match the final rendered text.
- Use
NSStringDrawingUsesLineFragmentOriginfor multi-line layout measurement. - Round measured sizes up with
ceilto avoid clipped text.

