@interface ViewController ()<UITextViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"www.google.com"];
NSDictionary *linkDic = @{ NSLinkAttributeName : [NSURL URLWithString:@"http://www.google.com"] };
[str setAttributes:linkDic range:[[str string] rangeOfString:@"www.google.com"]];
_textView.attributedText = str;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(@"=============%@",URL);
return YES;
}
须要注意的是 将上述代码 需作以下调整 咱们须要的是 点击跳转 而不是点击编辑 因此须要关闭编辑属性
以下:
在 IB 中设置如下 > 实用程序 > 属性检查器。值得注意的是,UITextView不能是可编辑,启用连接。google
你也能够作一样的代码:spa
_textView.editable = NO;
_textView.dataDetectorTypes = UIDataDetectorTypeLink;