UITextView link detection in iOS 7

2019-01-08 21:29发布

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.

17条回答
地球回转人心会变
2楼-- · 2019-01-08 21:42

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.

查看更多
我命由我不由天
3楼-- · 2019-01-08 21:43

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.

查看更多
Deceive 欺骗
4楼-- · 2019-01-08 21:46

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

查看更多
叛逆
5楼-- · 2019-01-08 21:48

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.

查看更多
倾城 Initia
6楼-- · 2019-01-08 21:51

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;
查看更多
在下西门庆
7楼-- · 2019-01-08 21:52

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.

查看更多
登录 后发表回答