2013-02-22 2 views
2

이 코드를 가지고 :탭 제스처

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

이 전체 UITextView에 반응하지만, 그것은 단지 문자열의 경우 특정 부분에 응답하도록 변경 할 수 있습니다 UITextView에서 도청? 예를 들어 URL처럼?

답변

7

정상적인 UITextView에서 특정 문자열에 탭 제스처를 할당 할 수 없습니다. UITextView에 대해 dataDetectorTypes를 설정할 수 있습니다. UIKit DataTypes Reference :

textview.dataDetectorTypes = UIDataDetectorTypeAll; 

만 URL을 감지 할 경우에 할당 할 수있는,

textview.dataDetectorTypes = UIDataDetectorTypeLink; 

이에 대한 자세한 내용은 설명서를 참조하십시오. 다음과 같이 확인, 당신의 의견을 바탕으로

: this에서

- (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 
} 

사용 : 당신은 입도 등으로

- (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]; 
} 

시도 할 수 있습니다 또한이 Documentation on UITextView

업데이트를 확인 as, UITextGranularitySentence, UITextGranularityLine 등을 확인하십시오.을 확인하십시오.210 여기.

+0

링크를 표시하지만 제스처가 모든 textView에 반응합니다 –

+0

예. 링크를 누르면 URL 스키마를 기반으로 해당 애플리케이션이 열립니다. – iDev

+0

그래, UITapGesture가있는 사파리에서 URL을 열면 내 UIWebView에 열어두기를 바랬다 –

관련 문제