【问题标题】:UITextView keep text styleUITextView 保持文本样式
【发布时间】:2014-12-30 00:38:37
【问题描述】:

为了允许用户与链接进行交互,我使用如下代码:

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";
});

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

据我了解,“修复”此问题的唯一方法是将字体、文本颜色等属性重置为初始值。

【问题讨论】:

标签: ios uitextview


【解决方案1】:

我找到了解决方法:

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";
}); 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2011-04-29
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多