iOS7 UITextView设置NSLinkAttributeName属性但无法单击

时间:2014-01-24 02:31:39

标签: ios uitextview nsattributedstring

enter image description here

@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;
}

有什么不对吗?

1 个答案:

答案 0 :(得分:10)

在IB&gt;中设置以下内容:公用事业&gt;属性检查员。值得注意的是,UITextView 无法可编辑并启用了链接。

UITextView Settings

您也可以使用代码执行相同操作:

_textView.editable = NO;
_textView.dataDetectorTypes = UIDataDetectorTypeLink;