Search code examples
iphoneobjective-ciosipad

How do I know if text exceeds the bounds of a text view?


I would like to know when text exceeds the bounds of a UITextView and requires the user to scroll. Knowing how many lines a certain font will occupy is useless - how do I know how many lines a certain amount of text will take up?

enter image description here

In the image above, I would add a fade effect so that I can cut off the text view gracefully. However, it does not look correct to display the fade if there is not enought text to scroll.


Solution

  • Get contentSize after setting the text. It's a CGSize and the height will be the height required for the UITextView to display the text without scrolling.

    myTextView.text = someText;
    CGSize textSize = myTextView.contentSize;
    float verticalSpaceNeededByText = textSize.height;