Last active
January 22, 2018 14:48
-
-
Save matt-curtis/f9efbd2c2df1b77e3471 to your computer and use it in GitHub Desktop.
Get number of lines in UITextView (iOS 7 compat.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how tested is this?