How to calculate the height of an NSAttributedString with given width in iOS 6

IosNsattributedstringBounding Box

Ios Problem Overview


> Possible Duplicate:
> How to get height for NSAttributedString at a fixed width

Now NSAttributedString is available in iOS 6. For layout purposes, I want to know how to calculate the required height of an NSAttributedString under fixed width. I'm looking for something that's equivalent to NSString's - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size but for NSAttributedString.

To calculate the drawing size of NSAttributedStrings, there are two methods available:

  1. - (CGSize)size can't be used because it does not take any width into consideration.
  2. I tried - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context, but somehow it doesn't give me the correct height. I think the method is buggy. If I run the following code, it gives me bounding size: 572.324951, 19.000000 ignoring the given width of 200. It should give me something like 100 of height.

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15], NSForegroundColorAttributeName : [UIColor blueColor]};
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];

CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(200, 1000) options:0 context:nil];
NSLog(@"bounding size: %f, %f", frame.size.width, frame.size.height);

There are other methods available for Mac OS X, but not for iOS.

Ios Solutions


Solution 1 - Ios

Option 2 does work in iOS with the proper parameters.

NSAttributedString *attrStr = ... // your attributed string
CGFloat width = 300; // whatever your desired width is
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

Without the proper values for the options parameter you will get the wrong height.

It is also required that attrStr contains a font attribute. Without a font, there is no way to properly calculate the size.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakeView Question on Stackoverflow
Solution 1 - IosrmaddyView Answer on Stackoverflow