title: How to calculate the height of an NSAttributedString with given width in iOS 6
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2282629/how-to-get-height-for-nsattributedstring-at-a-fixed-width">How to get height for NSAttributedString at a fixed width</a>  </p>
  </blockquote>
  
  
  
  <p>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 <code>- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size</code> but for NSAttributedString.</p>
  
  <p>To calculate the drawing size of NSAttributedStrings, there are two methods available:</p>
  
  <ol>
  <li><code>- (CGSize)size</code> can't be used because it does not take any width into consideration.</li>
  <li>I tried <code>- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context</code>, 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 <code>bounding size: 572.324951, 19.000000</code> ignoring the given width of 200. It should give me something like 100 of height.</li>
  </ol>
  
  <pre>
      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);
  </pre>
  
  <p>There are other methods available for Mac OS X, but not for iOS.</p>