2014-11-03 33 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"; 
}); 

但也許有更正確的/優雅的解決方案。