Skip to content

Instantly share code, notes, and snippets.

@wess
Last active December 13, 2019 17:33
Show Gist options
  • Save wess/5161817 to your computer and use it in GitHub Desktop.
Save wess/5161817 to your computer and use it in GitHub Desktop.
Getting visible range of text for a UITextView
@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