title: UITextView NSAttributedString Size
body: <p>I'm working on an app which uses a UITextView.</p>
  
  <p>The UITextView should grow or shrink to fit its text, both vertically and horizontally. To do this, I'm overriding sizeToFit in a subclass, and I set the bounds like so:</p>
  
  <pre><code>- (void)sizeToFit {
      [self setBounds:(CGRect){.size = self.attributedText.size}];
  }
  </code></pre>
  
  <p>The problem is that this size just doesn't reflect the correct size of the string, as the UITextView clips the text. I'm setting the edge insets to zero, so that shouldn't be an issue right?</p>
  
  <p>At this point, I think it's a bug with NSAttributedString's size property, but the same thing happens if I use boundingRectWithSize:options:context:</p>
  
  <pre><code>[self setBounds:(CGRect){.size = [self.attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:0 context:nil].size}];
  </code></pre>
  
  <p>So maybe whatever code is doing layout calculations for NSAttributedString doesn't play nicely with UITextView's layout calculations.</p>
  
  <p><a href="http://files.thermoglobalnuclearwar.com/TextViewBug.zip" rel="noreferrer">Here is example project which demonstrates the issue</a>.</p>
  
  <p>Any ideas are welcome!</p>
  
  <p><strong>EDIT: I should also point out that the following doesn't work either:</strong></p>
  
  <pre><code>- (void)sizeToFit {
      [self setBounds:(CGRect){.size = [self sizeThatFits:self.attributedText.size]}];
  }
  </code></pre>