UITextView link detection in iOS 7

IosObjective CIos6UitextviewIos7

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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 - Ios

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 - Ios

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 - Ios

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 - Ios

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;

Solution 6 - Ios

You should check out NSDataDetector.

You can use this to find and deal with different data (links, phone numbers and more). Have a look on this site:

http://nshipster.com/nsdatadetector/

You can also use the dataDetectorTypes property of UITextView to set what you want to detect in code. May just be a storyboard transition problem for you.

textView.dataDetectorTypes = UIDataDetectorTypeLink;

Solution 7 - Ios

Be aware, that your textview will only recognize the links if not editable!

Here is a nice tutorial on how to make an editable UITextView with `link detection``

Editable UITextView with link detecion

I've not experienced any problems with that solution since now.

The trick is a GestureRecognizer forwaring touches and enabling/disabling the editing.

You could apply the same thing with the selectable / not selectable issue on iOS7

Solution 8 - Ios

After few tests, I found solution.

If you want links active and you don't want selection enabled, you need to edit gestureRecognizers.

For example - there are 3 LongPressGestureRecognizers. One for click on link (minimumPressDuration = 0.12), second for zoom in editable mode (minimumPressDuration = 0.5), third for selection (minimumPressDuration = 0.8). This solution removes LongPressGestureRecognizer for selecting and second for zooming in editing mode.

NSArray *textViewGestureRecognizers = self.captionTextView.gestureRecognizers;
NSMutableArray *mutableArrayOfGestureRecognizers = [[NSMutableArray alloc] init];
for (UIGestureRecognizer *gestureRecognizer in textViewGestureRecognizers) {
    if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
    } else {
        UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
        if (longPressGestureRecognizer.minimumPressDuration < 0.3) {
            [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
        }
    }
}
self.captionTextView.gestureRecognizers = mutableArrayOfGestureRecognizers;

Tested on iOS 9, but it should work on all versions (iOS 7, 8, 9). I hope it helps! :)

Solution 9 - Ios

I've found the trick, this works in iOS 7!

You have to set the UITextView selectable in your xib or programmatically

self.yourTextView.selectable = YES;

and then you have to disable scrolls and enable again after set your text.

self.yourTextView.scrollEnabled = NO;
[self.yourTextView setText:contentString];
self.yourTextView.scrollEnabled = YES;

Solution 10 - Ios

So using a UITextView keeping it enabled, selectable, not scrollable & links detectable is not as simple as it seems. I encountered this in iOS 8. So my solution was to do something like this in viewDidLoad and then set editable property to NO when textBox editing is done(usually would be a method like doneIsTapped). The trick here is to set editable property to NO after setting text value to textview is completed. This will enable links in the UITextview.

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.txtViewComment.editable = YES;
    self.txtViewComment.selectable = YES;
    self.txtViewComment.dataDetectorTypes = UIDataDetectorTypeLink;
    self.txtViewComment.scrollEnabled = NO;
}

and

- (IBAction)doneIsTapped:(id)sender 
{
    self.txtViewComment.text = @"set text what ever you want";
    self.txtViewComment.editable = NO; 
}

this made the links enabled in textview. Also I would recommend not to use story board at this time(or until apple fixes this problem) and just use code to avoid any unnecessary confusion. Hope this help.

Solution 11 - Ios

Deactivating UITextViews scrolling ability did the trick for me in a similar setup.

Solution 12 - Ios

Changing the Tint color to other color actually works. However if selectable enable the tint will also be the same color.

Solution 13 - Ios

Make the scrolling property of UITextView to No. it will work... Self.textView.ScrollingEnable = NO;

Solution 14 - Ios

None of the above worked for me, instead I did this:

[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];
[self.textView.setTextColor:[UIColor whiteColor]];
[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];

I did this with my textview that was supposed to detect all types, and which had non detected color set to white. You can change the code to represent your proper color and link types to detect.

Solution 15 - Ios

While this thread is old, I didn’t see an answer that worked for me with Swift, so here goes for Swift 2.2

textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.selectable = true

Solution 16 - Ios

This workaround works for me:

textView.selectable = YES;
textView.delegate = self;

- (void) textViewDidChangeSelection:(UITextView *)textView;
{
    NSRange range = NSMakeRange(NSNotFound, 0.0);
    if ( range.length && !NSEqualRanges(range, textView.selectedRange) ) {
        textView.selectedRange = range;
    }
}

Solution 17 - Ios

If you are adding UITextview programmatically just add below lines:

        _textView.userInteractionEnabled = YES;
        _textView.dataDetectorTypes = UIDataDetectorTypeLink;
        _textView.scrollEnabled = NO;
        _textView.editable = NO;

This worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTobiasView Question on Stackoverflow
Solution 1 - IosTobiasView Answer on Stackoverflow
Solution 2 - IosTheo BendixsonView Answer on Stackoverflow
Solution 3 - IosRaconteurView Answer on Stackoverflow
Solution 4 - IossvarrallView Answer on Stackoverflow
Solution 5 - IosHarish PathakView Answer on Stackoverflow
Solution 6 - IosDanielView Answer on Stackoverflow
Solution 7 - IosAlexanderView Answer on Stackoverflow
Solution 8 - IosJakub KašparView Answer on Stackoverflow
Solution 9 - IosÁlvaro MurilloView Answer on Stackoverflow
Solution 10 - IosskypirateView Answer on Stackoverflow
Solution 11 - IosTharagonView Answer on Stackoverflow
Solution 12 - IosDesmondView Answer on Stackoverflow
Solution 13 - IosShaheen RehmanView Answer on Stackoverflow
Solution 14 - IosTundeView Answer on Stackoverflow
Solution 15 - IosSilviView Answer on Stackoverflow
Solution 16 - IosAndrey SolovievView Answer on Stackoverflow
Solution 17 - IosMaishi WadhwaniView Answer on Stackoverflow