Skip to content

Instantly share code, notes, and snippets.

@matt-curtis
Last active January 22, 2018 14:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-curtis/f9efbd2c2df1b77e3471 to your computer and use it in GitHub Desktop.
Save matt-curtis/f9efbd2c2df1b77e3471 to your computer and use it in GitHub Desktop.
Get number of lines in UITextView (iOS 7 compat.)
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextPosition *pos = textView.endOfDocument;
NSInteger lines = 0;
while (true){
UITextPosition *lineEnd = [tokenizer positionFromPosition:pos toBoundary:UITextGranularityLine inDirection:UITextStorageDirectionBackward];
if([textView comparePosition:pos toPosition:lineEnd] == NSOrderedSame){
pos = [tokenizer positionFromPosition:lineEnd toBoundary:UITextGranularityCharacter inDirection:UITextStorageDirectionBackward];
if([textView comparePosition:pos toPosition:lineEnd] == NSOrderedSame) break;
continue;
}
lines++; pos = lineEnd;
}
@dj9889
Copy link

dj9889 commented Apr 20, 2015

how tested is this?

@bogaevskyi
Copy link

It is not properly works when multiple end line character inside the text. This is solution works for me: http://stackoverflow.com/a/18818036/2739795

@darren90
Copy link

work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment