title: How to calculate TextView height base on text
body: <p>I am using the code below for calculate the height of text, then set this height for <code>UILabel</code> and <code>UITextView</code></p>
  
  <pre><code>CGSize targetSize = CGSizeMake(300, CGFLOAT_MAX);
  NSString *message = @"The Internet connection appears to be offline.";
  
  NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  CGSize boundingBox = [message boundingRectWithSize:targetSize
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName:FontOpenSanWithSize(14)}
                                                context:context].size;
  
  CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
  // it will return size:width = 299 and size height 20
  // =&gt; if I use this height and set for UILabel, it can display full content
  // =&gt; if I use this height and set for UITextView, it can not display full content 
  </code></pre>
  
  <p>It's work perfect for <code>UILabel</code> but for <code>UITextView</code> sometime it calculate wrong.<br>
  I think the problem happened because the padding (left, right) of <code>UITextView</code> is bigger than <code>UILabel</code>.<br>
  So how can I calculate the correct size of text for display in <code>UITextView</code>. Any help or suggestion would be great appreciated.      </p>
  
  <p>Like the description image below<br>
  With the same size (300), same font, same text but the <code>UITextView</code> display in 2 lines, but <code>UILabel</code> in 1 lines.
  And my code for calculate height return 20, it not enough for display in 2 lines, so the <code>UITextView</code>can not display full content</p>
  
  <p><strong>The reason why I need to calculate the height of <code>UITextView</code> base on text</strong> because my <code>UITextView</code> is in a popup.
  And the popup height will depend on the <code>TextView</code> height</p>
  
  <p><a href="https://i.stack.imgur.com/TFyay.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TFyay.png" alt="enter image description here"></a></p>