为什么要使用点击手势选择链接?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;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction
{
if ([URL.absoluteString isEqualToString:@"ShowInfoLink"]) {
// Do something
}
return NO;
}
要使其工作,您必须设置selectable=YES,并在情节提要中选中links标志,以便能够检测链接。