Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 924
Jack Humphries Avatar asked Oct 24 '25 06:10

Jack Humphries


2 Answers

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;
like image 73
Hot Licks Avatar answered Oct 25 '25 21:10

Hot Licks


Updated for Swift 5.x

func textExceedBoundsOf(_ textView: UITextView) -> Bool {
    textView.contentSize.height > self.textView.bounds.height
}
like image 22
Adrian Avatar answered Oct 25 '25 19:10

Adrian