Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView link detection in iOS 7

I have a UITextView which is managed via Interface Builder. As data detection I have "Links" checked. In iOS 6 everything is working fine and links are highlighted and are clickable. In iOS 7 though, all links remain just plain text. The editable and selectable checkboxes are unchecked.

What may be of concern is that the UITextView is a subview of a container view which is again inside a UIScrollView.

like image 451
Tobias Avatar asked Sep 23 '13 15:09

Tobias


4 Answers

It seems that in iOS 7 link detection only works if the UITextView is selectable. So making my UITextView not selectable stopped the the link detection from working.

I also tested this in iOS 6 and I can confirm that in iOS 6 the link detection works fine even with the UITextView not being selectable.

like image 67
Tobias Avatar answered Nov 05 '22 10:11

Tobias


I was having some problems with phone number detection today. It seemed like the UITextView would retain old phone numbers and keep text highlighted after I had set the text to something else.

I found that if I setText:nil before setting the text to the new string, it would reset the textview, and phone numbers would highlight as normal. I'm wondering if this is some kind of bug with UITextView in iOS 7.0

Either way, this did work for me.

like image 32
Theo Bendixson Avatar answered Nov 05 '22 10:11

Theo Bendixson


When iOS7 first came out this plagued me and I found an answer in this thread (setting the text attribute of the UITextView to nil before setting the actual value did the trick). Then suddenly, the problem (for me it was the entire string being highlighted as a link) cropped back up (assumedly due to an iOS update).

What finally did the trick for me was to stop using the text attribute and set the attributedText. Once I did this, the need for setting fonts/scrolling/selectable/editable/etc. programmatically, disappeared. I defined my UITextView in IB, set the values as I wanted (not scrollable, not editable, selectable, detecting links and phone numbers) and then built an attributed string and set:

myUITextView.attributedString = myAttributedString;

And suddenly everything worked as expected. Hope this helps someone else down the road.

like image 26
Raconteur Avatar answered Nov 05 '22 10:11

Raconteur


I had the same issue and disabling scrolling on the UITextView activates the link detection on load rather than only working once the user has interacted with the textview. The UITextView also had to be selectable and non-editable.

detailTextView.scrollEnabled = NO;
detailTextView.editable = NO;
detailTextView.selectable = YES;

Being selectable or having scroll enabled isn't necessary on iOS6.

Another thing to check is that userinteraction is enabled on the cell and content view of the cell, otherwise the link won't be clickable.

like image 18
svarrall Avatar answered Nov 05 '22 10:11

svarrall