直接上代码:
NSMutableAttributedString *atbs =[[NSMutableAttributedString alloc] initWithAttributedString: self.textView.attributedText];
NSRange range = [[atbs string] rangeOfString:@"人工方式"];
[atbs addAttribute: NSLinkAttributeNamevalue:@"www.baidu.com" range: range];
self.textView.attributedText= atbs;
self.textView.delegate=self;
self.textView.editable=NO;
self.textView.selectable=YES;//是否可以选择一定要打开,不然不能响应点击事件,网上的代码大都是关闭状态,误导了好多人!
代理回调:
#pragma mark--textViewDelegate
- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
NSLog(@"点击响应---------------");
return YES;
}
在模拟器上的点击比普通的按钮点击要稍微加长点时间,待文字背景变灰后就会回调shouldInteractWithURL.