I wanted to know when a text is wrapped by the frame of the text view is there any delimiter with which we can identify whether the text is wrapped or not.
For instance if my text view has a width of 50 px and text is exceeding that, it wraps the text to next line.
I wanted to count the number of lines in my text view. Now "\n" and "\r" are not helping me.
My code is:
NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\n\r"];
NSArray *myArray = [textViewText componentsSeparatedByCharactersInSet:aCharacterSet];
NSLog(@"%d",[myArray count]);
This variation takes into account how you wrap your lines and the max size of the
UITextView
, and may output a more precise height. For example, if the text doesn't fit it will truncate to the visible size, and if you wrap whole words (which is the default) it may result in more lines than if you do otherwise.Swift 4 version of Luke Chase's answer
Swift 3:
Swift extension:
Using @himanshu padia answer
Usage :
yourTextView.numberOfLines()
be aware that if for some reason the font of the text view is nil, the return will be zero.
You need to consider textView.textContainerInset, also need to round the calculated value since line number definitely is an integer
In real case, you may see following result rawLineNumber = 3.008099 finalLineNumber = 3 (3 lines)