質問

I am trying to find the Tapped String in UITextView. i have tried UIDataDetectorTypeLink.but it is not working. also i am not sure is it possible to detect the String. I am using a UITextView in a custom UITableViewCell. from that i am trying to detect the tapped String. also below are my sample code which i tried. can anyone please help me to find what have i done wrong and how can i solve this ? Even if it is not possible then how can i solve this issue ?

NSMutableArray *sampleArray = [[NSMutableArray alloc]init];
NSMutableString *mutableString = [[NSMutableString alloc]init];
NSMutableAttributedString * string;
sampleArray = [[postArray valueForKey:@"hash_tags"] objectAtIndex:indexPath.row];
for(NSString *sampleString in sampleArray){
    if (mutableString.length == 0) {
        [mutableString appendString:sampleString];
    }else{
        [mutableString appendString:[NSString stringWithFormat:@", %@",sampleString]];
    }
    string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",mutableString]];
    //        [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,[string length])];
}
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:5.0f/255.0f green:107.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSUnderlineColorAttributeName: [UIColor greenColor],NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
[string addAttribute:NSLinkAttributeName
               value:string
               range:NSMakeRange(0, [string length])];//[[string string] rangeOfString:sampleString]];
[cell.tagsTextView setAttributedText:string];
[cell.tagsTextView setDelegate:self];
[cell.tagsTextView setUserInteractionEnabled:YES];
cell.tagsTextView.scrollEnabled = NO;
cell.tagsTextView.editable = NO;
cell.tagsTextView.dataDetectorTypes = UIDataDetectorTypeLink;
//        cell.tagsTextView.textContainer.lineFragmentPadding = 0;
//        cell.tagsTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
[cell.tagsTextView setLinkTextAttributes:linkAttributes];

UITextView Delegates

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    NSLog(@"url %@",URL);
    if ([[URL scheme] isEqualToString:@"username"]) {
        NSString *username = [URL host];
        // do something with this username
        // ...
        return NO;
    }
    return YES; // let the system open this URL
}

Thanks in Advance

役に立ちましたか?

解決

Added UITapGestureRecognizer to the UITextView and below is the UITapGestureRecognizerSelector method.

- (void) tapResponse:(UITapGestureRecognizer *)recognizer{

    UITextView *textView =  (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:textView];
    NSLog(@"Tap Gesture Coordinates: %.2f %.2f -- %@", location.x, location.y,textView.text);

    CGPoint position = CGPointMake(location.x, location.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:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
    NSString *tappedSentence = [textView textInRange:textRange];//[self lineAtPosition:CGPointMake(location.x, location.y)];
    NSLog(@"selected :%@ -- %@",tappedSentence,tapPosition);
}

他のヒント

Thanks to @Grey_Code, the Swift 4 version of this code is:

@IBAction func objectsTapLabel(gesture: UITapGestureRecognizer) {
    let text = (YourTextView.text)!

    let location = gesture.location(in: YourTextView)
    let position = CGPoint(x: location.x, y: location.y)
    let tapPosition = YourTextView.closestPosition(to: position)
    let tappedRange = YourTextView.tokenizer.rangeEnclosingPosition(tapPosition!, with: UITextGranularity.word, inDirection: 1)
    var tappedWord: String?
    if tappedRange != nil {
        tappedWord = messageTV.text(in: tappedRange!)
    }
    print("tapped word: \(tappedWord)")
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top