UITextView link detection in iOS 7

52,642

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

Check These Lines must be added to use data detector property of textview in UItableView cell.

    txtvwMsgText.userInteractionEnabled = YES;
    txtvwMsgText.dataDetectorTypes = UIDataDetectorTypeLink;
    txtvwMsgText.scrollEnabled = NO;
    txtvwMsgText.editable = NO;
    txtvwMsgText.selectable = YES;
Share:
52,642
Tobias
Author by

Tobias

Former iOS developer. Currently PhD student working on adaptive mobile learning software.

Updated on July 08, 2022

Comments

  • Tobias
    Tobias almost 2 years

    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.