Click gesture into the UITextView part

I have this code:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponse)]; singleTap.numberOfTapsRequired = 1; [_textView addGestureRecognizer:singleTap]; 

This will respond to the entire UITextView, but is it possible to change it so that it only responds when some part of the string in the UITextView is used? Like for example a url?

+2
ios nsstring uitextview uitapgesturerecognizer
Feb 22 '13 at 23:02
source share
1 answer

You cannot assign a binding gesture to a specific row in a regular UITextView. You can probably set dataDetectorTypes for a UITextView.

 textview.dataDetectorTypes = UIDataDetectorTypeAll; 

If you want to detect only URLs, you can assign

 textview.dataDetectorTypes = UIDataDetectorTypeLink; 

For more information, see the documentation: DataTypes UIKit Data Reference . Also check out the UITextView Documentation

Update:

Based on your comment, check the following:

 - (void)tapResponse:(UITapGestureRecognizer *)recognizer { CGPoint location = [recognizer locationInView:_textView]; NSLog(@"Tap Gesture Coordinates: %.2f %.2f", location.x, location.y); NSString *tappedSentence = [self lineAtPosition:CGPointMake(location.x, location.y)]; //use your logic to find out whether tapped Sentence is url and then open in webview } 

From this use:

 - (NSString *)lineAtPosition:(CGPoint)position { //eliminate scroll offset position.y += _textView.contentOffset.y; //get location in text from textposition at point UITextPosition *tapPosition = [_textView closestPositionToPoint:position]; //fetch the word at this position (or nil, if not available) UITextRange *textRange = [_textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularitySentence inDirection:UITextLayoutDirectionRight]; return [_textView textInRange:textRange]; } 

You can try with details like UITextGranularitySentence, UITextGranularityLine, etc. Here's the documentation .

+7
Feb 22 '13 at 23:04
source share



All Articles