I have a UITextView displaying non-editable text. I want the text to automatically parse links, phone numbers, etc for the user, and for those to be clickable.
I don't want the user to be able to highlight text, though, because I want to override those long press and double-tap interactions to do something different.
In order for links to be parsed in iOS7, the Selectable switch needs to be turned on for the UITextView, but Selectable also enables highlighting, which I don't want.
I tried overriding the LongPress gesture to prevent highlighting, but that seems to have disabled ordinary taps on links as well...
for (UIGestureRecognizer *recognizer in cell.messageTextView.gestureRecognizers) {
if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
recognizer.enabled = NO;
}
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]){
recognizer.enabled = YES;
}
}
There are lots of similar threads out there but none seem to address this specific question of links enabled, text not highlightable.
I am working on the exact same problem and the best I could do was to instantly clear the selection as soon as it is made by adding the following to the UITextView's delegate:
Note the check to prevent recursion. This pretty much addresses the issue because only selection is disabled -- links will still work.
Another tangential issue is that the text view will still become first responder, which you can fix by setting your desired first responder after setting the selected range.
Note: the only visual oddity that remains is that press-and-hold brings up the magnifying glass.
Here's a UITextView subclass approach that will analyze its gesture recognizers and only allow those that interact with linked text (using Swift 3).
This pretty much addresses the issue as text selection is disabled and hides magnifying glass -- links will still work.
Note: Instead of removing, you can replace the recognizer with your desired one.
As I wrote on the other post, there is another solution.
After few tests, I found solution.
If you want links active and you won't 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.
Tested on iOS 9, but it should work on all versions (iOS 7, 8, 9). I hope it helps! :)
Here's what worked for me.
I couldn't get rid of the magnify glass, but this will allow you to keep the text view selectable (so you can tap the links), but get rid of all the selection related UI. Only tested on iOS 9.
Caution Swift below!
First, subclass
UITextView
and include this function:That will disable the copy, etc menu. I then include a setup method, which I call from init, where I do a bunch of setup related tasks. (I only use these text views from a storyboard, thus the decoder init):
Selectable = true to keep the links tappable, editable = false because links aren't tappable in an editable text view. Specifying a clear
tintColor
hides the blue bars that appear at the beginning and end of a selection.Lastly, in the controller that is using the subclassed text view, make sure the
UITextViewDelegate
protocol is included, that the delegate is settextView.delegate = self
, and implement this delegate function:Without this function, the selection bars, and contextual menu will be disabled, but a colored background will still be left behind the text you selected. This function gets rid of that selection background.
Like I said, I haven't found a way to get rid of the magnify glass, but if they do a long tap anywhere besides a link, nothing will be left behind once the magnify glass disappears.
Swift 4, Xcode 9.2
Below is something different approach ,
HOW TO USE,