iOS7中UITextView设置NSLinkAttributeName属性但无法点击

3

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个回答

10

在IB > 工具 > 属性检查器中设置以下内容。特别注意UITextView无法同时启用编辑和链接。

UITextView Settings

您也可以使用代码实现此操作:

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

就这样了!非常感谢! - jxdwinter
4
必须将textView.selectable设置为YES,否则这将无法工作。 - Wirsing
1
感谢@Wirsing提供的额外提示!很烦人的是这意味着用户也可能会意外选择文本... - jowie
不客气,这个问题也困扰了我。没错,它确实很烦人! - Wirsing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接