title: Getting NSTextView to perfectly fit its contents
body: <p>I have a view that contains a button and a textview. When the button is clicked, the textview's hidden status will change and be shown on the view. Springs and struts have been configured so the textview expands vertically with the view. All this is done in IB</p>
  
  <p>I then insert text into the textview programmatically, but I need the textview to show all its contents without the user needing to scroll. </p>
  
  <p>This is the code I use to calculate the height of the text in the textview:</p>
  
  <pre><code>- (float) getTextViewHeight {
  
      //based on http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html%23//apple_ref/doc/uid/20001809-CJBGBIBB
  
      [textview.textContainer setLineFragmentPadding:0.0];
      [textview.layoutManager glyphRangeForTextContainer:textview.textContainer]; 
  
      return [textview.layoutManager usedRectForTextContainer:self.interactionData.textContainer].size.height;    
  }
  </code></pre>
  
  <p>With or without that call to -sizeToFit on the textview, it will either be too big or too small (depending on its contents).</p>
  
  <p>I need to get the height of the textview with all the contents showing so I can adjust the view's size.</p>
  
  <p>I know I could probably use a NSTextField as a label, but I need a NSTextView for its added functionality (specifically using the enclosing scrollview's rulerview).</p>
  
  <p>Does anybody have any suggestions?</p>