您不能在正常的UITextView中将轻敲手势分配给特定的字符串。你可以设置UITextView的dataDetectorTypes。
textview.dataDetectorTypes = UIDataDetectorTypeAll;
如果你想只检测URL,可以分配到,
textview.dataDetectorTypes = UIDataDetectorTypeLink;
检查文档以更多的细节:UIKit DataTypes Reference。还要检查这个Documentation on UITextView
更新:
基于您的评论,检查是这样的:
- (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
}
从this,用途:
- (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];
}
您可以用粒度这种尝试如UITextGranularitySentence,UITextGranularityLine等。检查documentation在这里。
即会显示链接,但手势会对所有文字作出反应 – 2013-02-22 23:08:42
是的。一旦你点击链接,它会根据url方案打开相应的应用程序。 – iDev 2013-02-22 23:09:20
是的,但它带有一个在Safari浏览器中打开的URL,使用UITapGesture,我希望能够在我的UIWebView中打开它。 – 2013-02-22 23:10:06