【发布时间】: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 将 editable 和 selectable 都设置为 NO。我知道有人说selctable 需要设置为YES,但我不确定这是真的。首先,它在 iOS 10 中没有可选择的情况下可以正常工作。其次,如果我将它设置为可选择的,它确实可以工作,但只是有点。我确实在 iOS 9 中获得了水龙头,但它只能正常工作(在 9 和 10 中)。有时它会记录水龙头,有时它不会。基本上,当我看到链接突出显示时,就像您在浏览器中单击链接时它不会注册。此外,现在可以选择我不想要的文本视图中的文本。
【问题讨论】:
标签: ios objective-c uitextview ios9 nsattributedstring