iOS 7
sizeWithAttributes
UILabel
text measurement
Apple development

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:

objective-c
1NSString *text = @"This is a long piece of text that may wrap onto multiple lines.";
2UIFont *font = [UIFont systemFontOfSize:16.0];
3CGSize maxSize = CGSizeMake(200.0, CGFLOAT_MAX);
4
5CGRect rect = [text boundingRectWithSize:maxSize
6                                 options:NSStringDrawingUsesLineFragmentOrigin |
7                                         NSStringDrawingUsesFontLeading
8                              attributes:@{ NSFontAttributeName: font }
9                                 context:nil];
10
11CGSize measuredSize = CGSizeMake(ceil(rect.size.width), ceil(rect.size.height));
12NSLog(@"%@", NSStringFromCGSize(measuredSize));

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::

objective-c
1NSString *text = @"Short label";
2UIFont *font = [UIFont systemFontOfSize:16.0];
3
4CGSize size = [text sizeWithAttributes:@{ NSFontAttributeName: font }];

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:

objective-c
1NSString *text = @"Dynamic label text that can span multiple lines.";
2UIFont *font = [UIFont systemFontOfSize:15.0];
3CGFloat labelWidth = 240.0;
4
5CGRect rect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
6                                 options:NSStringDrawingUsesLineFragmentOrigin |
7                                         NSStringDrawingUsesFontLeading
8                              attributes:@{ NSFontAttributeName: font }
9                                 context:nil];
10
11CGFloat labelHeight = ceil(rect.size.height);

If you later assign that text to a UILabel, make sure the label itself is configured consistently:

  • same font
  • same width constraint
  • 'numberOfLines = 0 for 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:

objective-c
1NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
2style.lineBreakMode = NSLineBreakByWordWrapping;
3
4NSDictionary *attributes = @{
5    NSFontAttributeName: [UIFont systemFontOfSize:16.0],
6    NSParagraphStyleAttributeName: style
7};
8
9CGRect rect = [text boundingRectWithSize:CGSizeMake(200.0, CGFLOAT_MAX)
10                                 options:NSStringDrawingUsesLineFragmentOrigin
11                              attributes:attributes
12                                 context:nil];

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:

objective-c
CGFloat width = ceil(rect.size.width);
CGFloat height = ceil(rect.size.height);

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 NSStringDrawingUsesLineFragmentOrigin for multi-line layout measurement.
  • Round measured sizes up with ceil to avoid clipped text.

Course illustration
Course illustration

All Rights Reserved.