【问题标题】:Tapping a NSLinkAttributeName link in UITextView not working in iOS 9在 iOS 9 中点击 UITextView 中的 NSLinkAttributeName 链接不起作用
【发布时间】:2017-08-16 11:10:45
【问题描述】:

我有一个带有属性文本的UITextView,其设置如下:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is a message.\nClick here for more info"];
textView.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
NSRange linkRange = [attributedString.string rangeOfString:@"Click here for more info"];
[attributedString addAttribute:NSLinkAttributeName value:@"" range:linkRange];
textView.attributedText = attributedString;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoTapped:)];
[textView addGestureRecognizer:tapRecognizer

然后我像这样点击一下:

- (void)infoTapped:(UITapGestureRecognizer *)tapGesture {
    if (tapGesture.state != UIGestureRecognizerStateEnded) {
        return;
    }

    UITextView *textView = (UITextView *)tapGesture.view;
    CGPoint tapLocation = [tapGesture locationInView:textView];
    UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
    NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
    NSString *link = attributes[NSLinkAttributeName];

    if (link) {
        // Do stuff
    }
}

在 iOS 10 中这工作正常,我能够检测到 NSLinkAttributeName 属性。 但是,在 iOS 9 中,对 [textView closestPositionToPoint:tapLocation] 的调用返回 nil,我无法从那里做任何事情。

顺便说一句。我的 textview 将 editableselectable 都设置为 NO。我知道有人说selctable 需要设置为YES,但我不确定这是真的。首先,它在 iOS 10 中没有可选择的情况下可以正常工作。其次,如果我将它设置为可选择的,它确实可以工作,但只是有点。我确实在 iOS 9 中获得了水龙头,但它只能正常工作(在 9 和 10 中)。有时它会记录水龙头,有时它不会。基本上,当我看到链接突出显示时,就像您在浏览器中单击链接时它不会注册。此外,现在可以选择我不想要的文本视图中的文本。

【问题讨论】:

    标签: ios objective-c uitextview ios9 nsattributedstring


    【解决方案1】:

    为什么要使用点击手势选择链接? UITextView 具有完善的链接识别功能。我无法回答为什么您的解决方案在 iOS 9 上无法正常工作,但我可以建议您以另一种方式处理链接。这也适用于 iOS 9。

        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"This is a message.\nClick here for more info" attributes:nil];
    NSRange range = [str.string rangeOfString:@"Click here for more info"];
    // add a value to link attribute, you'll use it to determine what link is tapped
    [str addAttribute:NSLinkAttributeName value:@"ShowInfoLink" range:range];
    self.textView.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
    self.textView.attributedText = str;
    // set textView's delegate
    self.textView.delegate = self;
    

    然后实现UITextViewDelegate的链接相关方法:

    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction
    {
        if ([URL.absoluteString isEqualToString:@"ShowInfoLink"]) {
            // Do something
        }
        return NO;
    }
    

    要使其正常工作,您必须设置 selectable = YES 并检查情节提要中的链接标志才能检测链接。

    【讨论】:

    • 我真的希望 UITextView 中的文本不能选择,因为它有时会产生一些丑陋的行为,但由于没有它我无法使其工作,所以我选择了你的方法。跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 2018-02-18
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2018-03-16
    相关资源
    最近更新 更多