2014-11-03 48 views
0

要允许带有链接的用户交互我用这样的代码:的UITextView保持文本样式

UITextView *t1 = [UITextView new]; 
[self.view addSubview:t1]; 
t1.frame = self.view.frame; 

t1.font = [UIFont systemFontOfSize:16]; 
t1.text = @"go to http://apple.com"; 
t1.dataDetectorTypes = UIDataDetectorTypeLink; 
t1.editable = NO; 

但是,如果我需要设置新的文本,它重用风格:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    t1.text = @"test"; 
}); 

将显示“测试“像链接(蓝色)但没有链接行为(转到链接,上下文菜单)。我没有在文档中找到为什么这是合法的。

据我所知,“解决”这个唯一的方法是重置属性,如字体,文本颜色初始值。

+0

[UITextViews在iOS 7中的UITableView链接检测错误]的可能重复(http://stackoverflow.com/questions/19121367/uitextviews-in-a-uitableview-link-detection-bug-in-ios-7 ) – 2014-11-03 11:40:40

回答

0

我发现这个方法来解决:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    t1.dataDetectorTypes = UIDataDetectorTypeNone; 
    t1.dataDetectorTypes = UIDataDetectorTypeLink; 
    t1.text = @"another text with http://link.com link"; 
}); 

但也许有更正确的/优雅的解决方案。