【问题标题】:Detecting Tapped String in UITextView在 UITextView 中检测被敲击的字符串
【发布时间】:2014-07-01 09:15:11
【问题描述】:

我正在尝试在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 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
}

提前致谢

【问题讨论】:

  • 添加一个 TapGesture 识别器,禁用UITextView 的可编辑属性(您似乎已经这样做了),并使用UITextPositionUITextView 查找问题。还是只查找带有链接的NSAttributedString
  • 你所说的“Tapped String”是什么意思?你在找一个词吗?一行文字?某个子串?
  • 例如:如果UITextView 中的字符串类似于@John、@David、@Larme,那么如果用户点击字符串@John然后我需要检测到用户已经从UITextView 中点击了 @John 字符串。我怎样才能实现这个字符串检测?
  • @Larme - 谢谢Larme,我已经使用了TapGesture 并检测到了轻敲的字符串。它正在工作,我已经在下面回答了。

标签: ios objective-c uitableview nsstring uitextview


【解决方案1】:

UITextView 中添加了 UITapGestureRecognizer,下面是 UITapGestureRecognizerSelector 方法

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

【讨论】:

  • 如果positionlocation完全相同,为什么还要声明?
  • 如果 UITextView 是 not 可编辑和 is 可选择的。长按打开字符串复制菜单。 (iOS9)
  • 这在 UITextView 不可滚动时有效。如果滚动,字符串将不正确。
【解决方案2】:

感谢@Grey_Code,这段代码的 Swift 4 版本是:

@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)")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多