iOS7 UITextView设置NSLinkAttributeName属性,但不能单击
@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 不能被编辑,并且启用了链接。
你也可以用代码做同样的事情:
_textView.editable = NO; _textView.dataDetectorTypes = UIDataDetectorTypeLink;