ios - 在 UITextView 中检测点击的字符串

标签 ios objective-c uitableview nsstring uitextview

我试图在 UITextView 中找到 Tapped String。我试过 UIDataDetectorTypeLink。但它不起作用。我也不确定是否可以检测到 String。我在自定义 UITableViewCell 中使用 UITextView。从那我试图检测被窃听的字符串。下面也是我尝试过的示例代码。谁能帮我找出我做错了什么,我该如何解决? 即使不可能,我该如何解决这个问题?

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 委托(delegate)

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

提前致谢

最佳答案

已将 UITapGestureRecognizer 添加到 UITextView 下方是 UITapGestureRecognizer选择器方法

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

关于ios - 在 UITextView 中检测点击的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635628/

相关文章:

javascript - 在 Xcode 中收到引用错误,指出 undefined object 。

c# - 使用 C# 停止 Unity3d 中的线程

ios - 如何将具有一定半径的圆添加到我的 MKpointAnnotation IOS Swift

ios - TableView 不重新加载

ios - 如何从 TableView 行和核心数据实体中删除数据?

ios - 将 UIKit 的 UIScrollView 与 SpriteKit 结合使用

objective-c - 如何使用sqlite管理器在sqlite中创建外键

ios - NSLineBreakByWordWrapping中断哪些字符?

ios - XCode 4.5 中 ARC 的 Phantom 内存泄漏,其中肯定调用了 dealloc 或 Instruments 问题?

ios - 如何根据设备动态计算 UITableViewCell 的高度