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:
- - (CGSize)sizecan't be used because it does not take any width into consideration.
- 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 mebounding size: 572.324951, 19.000000ignoring 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.
 
     
    