Last active
December 13, 2019 17:33
-
-
Save wess/5161817 to your computer and use it in GitHub Desktop.
Getting visible range of text for a UITextView
This file contains hidden or 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
@implementation UITextView (Annex) | |
@dynamic visibleTextRange; | |
- (NSRange)visibleTextRange | |
{ | |
CGRect bounds = self.bounds; | |
CGSize textSize = [self.text sizeWithFont:self.font constrainedToSize:bounds.size]; | |
UITextPosition *start = [self characterRangeAtPoint:bounds.origin].start; | |
UITextPosition *end = [self characterRangeAtPoint:CGPointMake(textSize.width, textSize.height)].end; | |
NSUInteger startPoint = [self offsetFromPosition:self.beginningOfDocument toPosition:start]; | |
NSUInteger endPoint = [self offsetFromPosition:start toPosition:end]; | |
return NSMakeRange(startPoint, endPoint); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment