Skip to content

Instantly share code, notes, and snippets.

@matt-curtis
Last active January 22, 2018 14:48
Show Gist options
  • 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;
}
@darren90
Copy link

work?

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