Skip to content

Instantly share code, notes, and snippets.

@boundsj
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boundsj/05769a89c51cb565b7ec to your computer and use it in GitHub Desktop.
Save boundsj/05769a89c51cb565b7ec to your computer and use it in GitHub Desktop.
Tappable Attributed UITextView
// in a tap gesture recognizer handler in view controller
// assuming you have a text view in controller called "myTextView"
- (void)myTextViewTapped:(UITapGestureRecognizer *)recognizer {
// get tap location in view in question
CGPoint location = [recognizer locationInView:self.myTextView];
// adjust location for text view's offsets
location.x -= self.myTextView.textContainerInset.left;
location.y -= self.myTextView.textContainerInset.top;
// get the specific letter that was tapped
NSInteger characterIndex = [self.myTextView.layoutManager characterIndexForPoint:location
inTextContainer:self.positionStanceTextView.textContainer
fractionOfDistanceBetweenInsertionPoints:0];
int minimum = 0;
int maximum = 10;
// if tapped charater is within arbitrary range, do whatever you want
if (characterIndex >= minimum - && characterIndex <= maximum) {
// do something here (i.e. delegate callback)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment