Last active
August 29, 2015 14:05
-
-
Save boundsj/05769a89c51cb565b7ec to your computer and use it in GitHub Desktop.
Tappable Attributed UITextView
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
// 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