user4645956
user4645956

Reputation:

Check if text in UITextView went to new line due to word wrap

How can I check if the text in a UITextView went to the next line due to word wrap?

I currently have code to check if the user enters a new line (from the keyboard).

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if ( [text isEqualToString:@"\n"] ) {
        ...
    }
    return YES;
}

Upvotes: 3

Views: 3737

Answers (1)

bfich
bfich

Reputation: 383

You can use the contentSize.height property of UITextView to determine how 'tall' your textview is. You can then divide by the lineHeight to figure out the number of lines. @nacho4d provided this nifty line of code that does just that:

int numLines = textView.contentSize.height/textView.font.lineHeight;

Refer to this question for more info: How to read number of lines in uitextview

EDIT: So you could do something like this inside -textViewDidChange: in order to check for a line change.

int numLines = textView.contentSize.height / textView.font.lineHeight; 
if (numLines != numLinesInTextView){
    numLinesInTextView = numLines;//numLinesInTextView is an instance variable
    //then do whatever you need to do on line change
}

Upvotes: 5

Related Questions