UITextView hyperlink formatting/function remains even after text changed to text without any links

334 views Asked by At

I am using a UITextView that displays different text depending on the course of user actions. Some text includes hyperlinks and some does not, so I would like to retain the UITextView's default setting to detect links. However, once I switch back to text that does not have a hyperlink, the entire text field is converted into a hyperlink for some link from the previous text. It is entirely blue, and if the user clicks on it, the app opens Safari to go to the link that's no longer there. I've tried setting the text to nil before replacing with real replacement text, but this hasn't worked:

infoLabel.text = nil;

Neither has this

infoLabel.text = @"";

Also I have tried explicitly setting the dataDetector type property on UILabel when I change the text, but then the text color is black even though I have set it to another color. So if I do this I have to reset the text color every time I reset the data detector type property. Quite frustrating. This feels like a bug. What am I missing?

2

There are 2 answers

0
sunny On BEST ANSWER

From this previous answer, UITextViews in a UITableView link detection bug in iOS 7, it appears this was something of a bug in ios 7. The solutions there did not work for me, but what did work for me was creating a new UITextView for each set of text, since the first set of text was always fine. If someone has a reason (other than bug) for why this might be happening, I would love to know since I still cannot update or reset a UITextView without having this bug.

2
Tobias On

Do u use the UITextView inside a UITableView and dequeueReusableCellWithIdentifier:? Then you might get an "old" UITextView that you need to set "back to default". setting infoLabel.textColor and infoLabel.font worked for me:

 [infoLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
 [infoLabel.textColor = [UIColor whiteColor];