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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
work?