UITextView: Disable selection, allow links

IosUitextviewNsattributedstring

Ios Problem Overview


I have a UITextView which displays an NSAttributedString. The textView's editable and selectable properties are both set to false.

The attributedString contains a URL and I'd like to allow tapping the URL to open a browser. But interaction with the URL is only possible if the selectable attribute is set to true.

How can I allow user interaction only for tapping links, but not for selecting text?

Ios Solutions


Solution 1 - Ios

I find the concept of fiddling with the internal gesture recognizers a little scary, so tried to find another solution. I've discovered that we can override point(inside:with:) to effectively allow a "tap-through" when the user isn't touching down on text with a link inside it:

// Inside a UITextView subclass:
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {

    guard let pos = closestPosition(to: point) else { return false }

    guard let range = tokenizer.rangeEnclosingPosition(pos, with: .character, inDirection: .layout(.left)) else { return false }

    let startIndex = offset(from: beginningOfDocument, to: range.start)

    return attributedText.attribute(.link, at: startIndex, effectiveRange: nil) != nil
}   

This also means that if you have a UITextView with a link inside a UITableViewCell, tableView(didSelectRowAt:) still gets called when tapping the non-linked portion of the text :)

Solution 2 - Ios

Try it please:

func textViewDidChangeSelection(_ textView: UITextView) {
    textView.selectedTextRange = nil
}

Solution 3 - Ios

if your minimum deployment target is iOS 11.2 or newer

You can disable text selection by subclassing UITextView and forbidding the gestures that can select something.

The below solution is:

  • compatible with isEditable
  • compatible with isScrollEnabled
  • compatible with links

/// Class to allow links but no selection.
/// Basically, it disables unwanted UIGestureRecognizer from UITextView.
/// https://stackoverflow.com/a/49443814/1033581
class UnselectableTappableTextView: UITextView {

    // required to prevent blue background selection from any situation
    override var selectedTextRange: UITextRange? {
        get { return nil }
        set {}
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer is UIPanGestureRecognizer {
            // required for compatibility with isScrollEnabled
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer,
            tapGestureRecognizer.numberOfTapsRequired == 1 {
            // required for compatibility with links
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        // allowing smallDelayRecognizer for links
        // https://stackoverflow.com/questions/46143868/xcode-9-uitextview-links-no-longer-clickable
        if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer,
            // comparison value is used to distinguish between 0.12 (smallDelayRecognizer) and 0.5 (textSelectionForce and textLoupe)
            longPressGestureRecognizer.minimumPressDuration < 0.325 {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        // preventing selection from loupe/magnifier (_UITextSelectionForceGesture), multi tap, tap and a half, etc.
        gestureRecognizer.isEnabled = false
        return false
    }
}

if your minimum deployment target is iOS 11.1 or older

Native UITextView links gesture recognizers are broken on iOS 11.0-11.1 and require a small delay long press instead of a tap: https://stackoverflow.com/questions/46143868/xcode-9-uitextview-links-no-longer-clickable

You can properly support links with your own gesture recognizer and you can disable text selection by subclassing UITextView and forbidding the gestures that can select something or tap something.

The below solution will disallow selection and is:

  • compatible with isScrollEnabled
  • compatible with links
  • workaround limitations of iOS 11.0 and iOS 11.1, but loses the UI effect when tapping on text attachments

/// Class to support links and to disallow selection.
/// It disables most UIGestureRecognizer from UITextView and adds a UITapGestureRecognizer.
/// https://stackoverflow.com/a/49443814/1033581
class UnselectableTappableTextView: UITextView {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        // Native UITextView links gesture recognizers are broken on iOS 11.0-11.1:
        // https://stackoverflow.com/questions/46143868/xcode-9-uitextview-links-no-longer-clickable
        // So we add our own UITapGestureRecognizer.
        linkGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
        linkGestureRecognizer.numberOfTapsRequired = 1
        addGestureRecognizer(linkGestureRecognizer)
        linkGestureRecognizer.isEnabled = true
    }
    
    var linkGestureRecognizer: UITapGestureRecognizer!
    
    // required to prevent blue background selection from any situation
    override var selectedTextRange: UITextRange? {
        get { return nil }
        set {}
    }
    
    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
        // Prevents drag and drop gestures,
        // but also prevents a crash with links on iOS 11.0 and 11.1.
        // https://stackoverflow.com/a/49535011/1033581
        gestureRecognizer.isEnabled = false
        super.addGestureRecognizer(gestureRecognizer)
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == linkGestureRecognizer {
            // Supporting links correctly.
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        if gestureRecognizer is UIPanGestureRecognizer {
            // Compatibility support with isScrollEnabled.
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        // Preventing selection gestures and disabling broken links support.
        gestureRecognizer.isEnabled = false
        return false
    }
    
    @objc func textTapped(recognizer: UITapGestureRecognizer) {
        guard recognizer == linkGestureRecognizer else {
            return
        }
        var location = recognizer.location(in: self)
        location.x -= textContainerInset.left
        location.y -= textContainerInset.top
        let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        let characterRange = NSRange(location: characterIndex, length: 1)
        
        if let attachment = attributedText?.attribute(.attachment, at: index, effectiveRange: nil) as? NSTextAttachment {
            if #available(iOS 10.0, *) {
                _ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange, interaction: .invokeDefaultAction)
            } else {
                _ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange)
            }
        }
        if let url = attributedText?.attribute(.link, at: index, effectiveRange: nil) as? URL {
            if #available(iOS 10.0, *) {
                _ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange, interaction: .invokeDefaultAction)
            } else {
                _ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange)
            }
        }
    }
}

Solution 4 - Ios

As Cœur has said, you can subclass the UITextView overriding the method of selectedTextRange, setting it to nil. And the links will still be clickable, but you won't be able to select the rest of the text.

class PIUnselectableTextView: PITextView {
    override public var selectedTextRange: UITextRange? {
        get {
            return nil
        }
        set { }
    }
}

Solution 5 - Ios

Enable selectable so that links are tappable, then just unselect as soon as a selection is detected. It will take effect before the UI has a chance to update.

yourTextView.selectable = YES;//required for tappable links
yourTextView.delegate = self;//use <UITextViewDelegate> in .h

- (void)textViewDidChangeSelection:(UITextView *)textView {
    if (textView == yourTextView && textView.selectedTextRange != nil) {
        // `selectable` is required for tappable links but we do not want
        // regular text selection, so clear the selection immediately.
        textView.delegate = nil;//Disable delegate while we update the selectedTextRange otherwise this method will get called again, circularly, on some architectures (e.g. iPhone7 sim)
        textView.selectedTextRange = nil;//clear selection, will happen before copy/paste/etc GUI renders
        textView.delegate = self;//Re-enable delegate
    }
}

Now, in newer iOS versions if you press and hold and drag on a UITextView the cursor can now flash and flicker using the above method, so to solve this we will simply make the cursor and selections (highlights) clear by adjusting the tint color, and then setting the link color back to whatever we desire (since it was previously using the tint color as well).

UIColor *originalTintColor = textView.tintColor;
[textView setTintColor:[UIColor clearColor]];//hide selection and highlight which now appears for a split second when tapping and holding in newer iOS versions
[textView setLinkTextAttributes:@{NSForegroundColorAttributeName: originalTintColor}];//manually set link color since it was using tint color before

Solution 6 - Ios

The solution for only tappable links without selection.

  1. Subclass UITextView to handle gestures which makes it only tappable. Based on the answer from Cœur
class UnselectableTappableTextView: UITextView {

    // required to prevent blue background selection from any situation
    override var selectedTextRange: UITextRange? {
        get { return nil }
        set {}
    }

    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer,
            tapGestureRecognizer.numberOfTapsRequired == 1 {
            // required for compatibility with links
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }

        return false
    }

}
  1. Set up delegate to disable the .preview from 3D Touch. Taking the reference from hackingwithswift
class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        //...
        textView.delegate = self
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        UIApplication.shared.open(URL)

        // Disable `.preview` by 3D Touch and other interactions
        return false
    }
}

If you want to have a UITextView only for embedding the links without scrolling gesture, this can be a good solution.

Solution 7 - Ios

So after some research I've been able to find a solution. It's a hack and I don't know if it'll work in future iOS versions, but it works as of right now (iOS 9.3).

Just add this UITextView category (Gist here):

@implementation UITextView (NoFirstResponder)

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        
        @try {
            id targetAndAction = ((NSMutableArray *)[gestureRecognizer valueForKey:@"_targets"]).firstObject;
            NSArray <NSString *>*actions = @[@"action=loupeGesture:",           // link: no, selection: shows circle loupe and blue selectors for a second                                             @"action=longDelayRecognizer:",    // link: no, selection: no                                             /*@"action=smallDelayRecognizer:", // link: yes (no long press), selection: no*/                                             @"action=oneFingerForcePan:",      // link: no, selection: shows rectangular loupe for a second, no blue selectors                                             @"action=_handleRevealGesture:"];  // link: no, selection: no
            for (NSString *action in actions) {
                if ([[targetAndAction description] containsString:action]) {
                    [gestureRecognizer setEnabled:false];
                }
            }
    
        }
        
        @catch (NSException *e) {
        }
    
        @finally {
            [super addGestureRecognizer: gestureRecognizer];
        }
    }
}

Solution 8 - Ios

Here is an Objective C version of the answer posted by Max Chuquimia.

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    UITextPosition *position = [self closestPositionToPoint:point];
    if (!position) {
        return NO;
    }
    UITextRange *range = [self.tokenizer rangeEnclosingPosition:position
                                                withGranularity:UITextGranularityCharacter
                                                    inDirection:UITextLayoutDirectionLeft];
    if (!range) {
        return NO;
    }

    NSInteger startIndex = [self offsetFromPosition:self.beginningOfDocument
                                         toPosition:range.start];
    return [self.attributedText attribute:NSLinkAttributeName
                                  atIndex:startIndex
                           effectiveRange:nil] != nil;
}

Solution 9 - Ios

This works for me:

@interface MessageTextView : UITextView <UITextViewDelegate>

@end

@implementation MessageTextView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.delegate = self;
}

- (BOOL)canBecomeFirstResponder {
    return NO;
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    textView.selectedTextRange = nil;
    [textView endEditing:YES];
}

@end

Solution 10 - Ios

Swift 4, Xcode 9.2

Below is something different approach for link, make isSelectable property of UITextView to false

class TextView: UITextView {
    //MARK: Properties    
    open var didTouchedLink:((URL,NSRange,CGPoint) -> Void)?

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
    }

    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = Array(touches)[0]
        if let view = touch.view {
            let point = touch.location(in: view)
            self.tapped(on: point)
        }
    }
}

extension TextView {
    fileprivate func tapped(on point:CGPoint) {
        var location: CGPoint = point
        location.x -= self.textContainerInset.left
        location.y -= self.textContainerInset.top
        let charIndex = layoutManager.characterIndex(for: location, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        guard charIndex < self.textStorage.length else {
            return
        }
        var range = NSRange(location: 0, length: 0)
        if let attributedText = self.attributedText {
            if let link = attributedText.attribute(NSAttributedStringKey.link, at: charIndex, effectiveRange: &range) as? URL {
                print("\n\t##-->You just tapped on '\(link)' withRange = \(NSStringFromRange(range))\n")
                self.didTouchedLink?(link, range, location)
            }
        }

    }
}

HOW TO USE,

let textView = TextView()//Init your textview and assign attributedString and other properties you want.
textView.didTouchedLink = { (url,tapRange,point) in
//here goes your other logic for successfull URL location
}

Solution 11 - Ios

Here's a Swift 4 solution that allows taps to pass trough except for when a link is pressed;

In the parent view

private(set) lazy var textView = YourCustomTextView()

func setupView() {
    textView.isScrollEnabled = false
    textView.isUserInteractionEnabled = false
    
    let tapGr = UITapGestureRecognizer(target: textView, action: nil)
    tapGr.delegate = textView
    addGestureRecognizer(tapGr)
    
    textView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(textView)
    NSLayoutConstraint.activate(textView.edges(to: self))
}

The custom UITextView

class YourCustomTextView: UITextView, UIGestureRecognizerDelegate {

    var onLinkTapped: (URL) -> Void = { print($0) }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let gesture = gestureRecognizer as? UITapGestureRecognizer else {
            return true
        }
        
        let location = gesture.location(in: self)
        
        guard let closest = closestPosition(to: location), let startPosition = position(from: closest, offset: -1), let endPosition = position(from: closest, offset: 1) else {
            return false
        }
        
        guard let textRange = textRange(from: startPosition, to: endPosition) else {
            return false
        }
        
        let startOffset = offset(from: beginningOfDocument, to: textRange.start)
        let endOffset = offset(from: beginningOfDocument, to: textRange.end)
        let range = NSRange(location: startOffset, length: endOffset - startOffset)
        
        guard range.location != NSNotFound, range.length != 0 else {
            return false
        }
        
        guard let linkAttribute = attributedText.attributedSubstring(from: range).attribute(.link, at: 0, effectiveRange: nil) else {
            return false
        }
        
        guard let linkString = linkAttribute as? String, let url = URL(string: linkString) else {
            return false
        }
        
        guard delegate?.textView?(self, shouldInteractWith: url, in: range, interaction: .invokeDefaultAction) ?? true else {
            return false
        }
        
        onLinkTapped(url)
        
        return true
    }
}

Solution 12 - Ios

Swift 4.2

Simple

class MyTextView: UITextView {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        
        guard let pos = closestPosition(to: point) else { return false }
        
        guard let range = tokenizer.rangeEnclosingPosition(pos, with: .character, inDirection: UITextDirection(rawValue: UITextLayoutDirection.left.rawValue)) else { return false }
        
        let startIndex = offset(from: beginningOfDocument, to: range.start)
        
        return attributedText.attribute(NSAttributedString.Key.link, at: startIndex, effectiveRange: nil) != nil
    }
}

Solution 13 - Ios

Swift 3.0

For above Objective-C Version via @Lukas

extension UITextView {
        
        override open func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
            if gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self) {
                do {
                   let array = try gestureRecognizer.value(forKey: "_targets") as! NSMutableArray
                    let targetAndAction = array.firstObject
                    let actions = ["action=oneFingerForcePan:",
                                   "action=_handleRevealGesture:",
                                   "action=loupeGesture:",
                                   "action=longDelayRecognizer:"]
                    
                    for action in actions {
                         print("targetAndAction.debugDescription: \(targetAndAction.debugDescription)")
                        if targetAndAction.debugDescription.contains(action) {
                            gestureRecognizer.isEnabled = false
                        }
                    }
                    
                } catch let exception {
                    print("TXT_VIEW EXCEPTION : \(exception)")
                }
                defer {
                    super.addGestureRecognizer(gestureRecognizer)
                }
            }
        }
        
    }

Solution 14 - Ios

I ended up combining solutions from https://stackoverflow.com/a/44878203/2015332 and https://stackoverflow.com/a/49443814/2015332 (iOS < 11 variant). This works as expected: a read-only, non selectable UITextView on which hyperlinks are still working. One of the advantages from Coeur's solution is that touch detection is immediate and does not display highlight nor allow drag&drop of a link.

Here is the resulting code:

class HyperlinkEnabledReadOnlyTextView: UITextView {
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        isEditable = false
        isSelectable = false
        initHyperLinkDetection()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        isEditable = false
        isSelectable = false
        initHyperLinkDetection()
    }
    
    
    
    // MARK: - Prevent interaction except on hyperlinks
    
    // Combining https://stackoverflow.com/a/44878203/2015332 and https://stackoverflow.com/a/49443814/1033581
    
    private var linkGestureRecognizer: UITapGestureRecognizer!
    
    private func initHyperLinkDetection() {
        // Native UITextView links gesture recognizers are broken on iOS 11.0-11.1:
        // https://stackoverflow.com/questions/46143868/xcode-9-uitextview-links-no-longer-clickable
        
        // So we add our own UITapGestureRecognizer, which moreover detects taps faster than native one
        linkGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
        linkGestureRecognizer.numberOfTapsRequired = 1
        addGestureRecognizer(linkGestureRecognizer)
        linkGestureRecognizer.isEnabled = true // because previous call sets it to false
    }
    
    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
        // Prevents drag and drop gestures, but also prevents a crash with links on iOS 11.0 and 11.1.
        // https://stackoverflow.com/a/49535011/1033581
        gestureRecognizer.isEnabled = false
        super.addGestureRecognizer(gestureRecognizer)
    }
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        // Allow only taps located over an hyperlink
        var location = point
        location.x -= textContainerInset.left
        location.y -= textContainerInset.top
        guard location.x >= bounds.minX, location.x <= bounds.maxX, location.y >= bounds.minY, location.y <= bounds.maxY else { return false }
        
        let charIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        return attributedText.attribute(.link, at: charIndex, effectiveRange: nil) != nil
    }
    
    @objc private func textTapped(recognizer: UITapGestureRecognizer) {
        guard recognizer == linkGestureRecognizer else { return }
        
        var location = recognizer.location(in: self)
        location.x -= textContainerInset.left
        location.y -= textContainerInset.top
        guard location.x >= bounds.minX, location.x <= bounds.maxX, location.y >= bounds.minY, location.y <= bounds.maxY else { return }

        let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        let characterRange = NSRange(location: characterIndex, length: 1)
        
        if let attachment = attributedText?.attribute(.attachment, at: index, effectiveRange: nil) as? NSTextAttachment {
            if #available(iOS 10.0, *) {
                _ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange, interaction: .invokeDefaultAction)
            } else {
                _ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange)
            }
        }
        
        if let url = attributedText?.attribute(.link, at: characterIndex, effectiveRange: nil) as? URL {
            if #available(iOS 10.0, *) {
                _ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange, interaction: .invokeDefaultAction)
            } else {
                _ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange)
            }
        }
    }
}

Please not that I had some trouble compiling the .attachment enum case, I removed it because I'm not using it.

Solution 15 - Ios

An ugly but a goodie.

private class LinkTextView: UITextView {
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        []
    }

    override func caretRect(for position: UITextPosition) -> CGRect {
        CGRect.zero.offsetBy(dx: .greatestFiniteMagnitude, dy: .greatestFiniteMagnitude)
    }
}

Tested with a text view where scrolling was disabled.

Solution 16 - Ios

Overide UITextView like below and use it to render tappable link with preserving html styling.

public class LinkTextView: UITextView {

override public var selectedTextRange: UITextRange? {
    get {
        return nil
    }
    set {}
}

public init() {
    super.init(frame: CGRect.zero, textContainer: nil)
    commonInit()
}

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}
    
private func commonInit() {
    self.tintColor = UIColor.black
    self.isScrollEnabled = false
    self.delegate = self
    self.dataDetectorTypes = []
    self.isEditable = false
    self.delegate = self
    self.font = Style.font(.sansSerif11)
    self.delaysContentTouches = true
}


@available(iOS 10.0, *)
public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Handle link
    return false
}

public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    // Handle link
    return false
}

}

Solution 17 - Ios

Here's how I solved this problem- I make my selectable textview a subclass that overrides canPerformAction to return false.

class CustomTextView: UITextView {

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }

}

Solution 18 - Ios

What I do for Objective C is create a subclass and overwrite textViewdidChangeSelection: delegate method, so in the implementation class:

#import "CustomTextView.h"

@interface CustomTextView()<UITextViewDelegate>
@end

@implementation CustomTextView

. . . . . . .

- (void) textViewDidChangeSelection:(UITextView *)textView
{
    UITextRange *selectedRange = [textView selectedTextRange];
    NSString *selectedText = [textView textInRange:selectedRange];
    if (selectedText.length > 1 && selectedText.length < textView.text.length)
    {
        textView.selectedRange = NSMakeRange(0, 0);
    }
}

Don't forget to set self.delegate = self

Solution 19 - Ios

@Max Chuquimia answer will solve the problem. But double tap will still show option menu of textView. Just add this below code inside your custom view.

override func canPerformAction(_ action: Selector, withSender sender: (Any)?) -> Bool {

       UIMenuController.shared.hideMenu()
       //do not display the menu
       self.resignFirstResponder()
       //do not allow the user to selected anything
       return false
}

Solution 20 - Ios

SWIFT 5

Here's a combination of the different answers and comments that worked for me:

Subclass of UITextView:

class DescriptionAndLinkTextView: UITextView {
    
    // MARK: - Initialization

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        dataDetectorTypes = .all
        backgroundColor = .clear
        isSelectable = true
        isEditable = false
        isScrollEnabled = false
        contentInset = .zero
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0
        linkTextAttributes = [.foregroundColor: UIColor.red,
                              .font: UIFont.systemFontSize,
                              .underlineStyle: 0,
                              .underlineColor: UIColor.clear]
    }

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        guard super.point(inside: point, with: event) else { return false }
        guard let pos = closestPosition(to: point) else { return false }
        guard let range = tokenizer.rangeEnclosingPosition(pos, with: .character, inDirection: .layout(.left)) else { return false }
        let startIndex = offset(from: beginningOfDocument, to: range.start)
        guard startIndex < self.attributedText.length - 1 else { return false } // to handle the case where the text ends with a link and the user taps in the space after the link.
        return attributedText.attribute(.link, at: startIndex, effectiveRange: nil) != nil
    }
    
}

How to use it (in this case, in a tableview cell):

class MyTableViewCell: UITableViewCell {
    
    // MARK: - IBOutlets
    @IBOutlet weak var infoTextView: DescriptionAndLinkTextView! {
        didSet {
            infoTextView.delegate = self
        }
    }
    
    // MARK: - Lifecycle
    override func awakeFromNib() {
        super.awakeFromNib()
        selectionStyle = .none
    }
    
}

// MARK: - UITextViewDelegate

extension MyTableViewCell: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        DispatchQueue.main.async {
            UIApplication.shared.open(URL)
        }
        // Returning false, to prevent long-press-preview.
        return false
    }
    
    func textViewDidChangeSelection(_ textView: UITextView) {
        if (textView == infoTextView && textView.selectedTextRange != nil) {
            // `selectable` is required for tappable links but we do not want
            // regular text selection, so clear the selection immediately.
            textView.delegate = nil // Disable delegate while we update the selectedTextRange otherwise this method will get called again, circularly, on some architectures (e.g. iPhone7 sim)
            textView.selectedTextRange = nil // clear selection, will happen before copy/paste/etc GUI renders
            textView.delegate = self // Re-enable delegate
        }
    }
}

Solution 21 - Ios

My solution is as follows by Cœur solution.

My problem is to add a clickable link for UITableViewCell with no 3D preview. My solution might help those who are looking for a solution to tableView.

For that, I just need to add delegate to my TextView variable from my tableView which is a UITableViewCell instance variable. Here is my tableView code

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell else {
               return UITableViewCell()
         }
         cell.update(text: text)
         cell.textView.delegate = self
         return cell
    }

Here is my custom TaleViewCell

final class TableViewCell: UITableViewCell, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!
    func update(text: text) {
        textView.isEditable = false
        textView.isUserInteractionEnabled = true
    }

}

Here is extension

extension UITextView {
        // To prevent blue background selection from any situation
        open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    
            if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer,
               tapGestureRecognizer.numberOfTapsRequired == 1 {
                // required for compatibility with links
                return super.gestureRecognizerShouldBegin(gestureRecognizer)
            }
            return false
        }
    }

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
QuestionlukasView Question on Stackoverflow
Solution 1 - IosMax ChuquimiaView Answer on Stackoverflow
Solution 2 - IosCarson VoView Answer on Stackoverflow
Solution 3 - IosCœurView Answer on Stackoverflow
Solution 4 - IosPablo Sanchez GomezView Answer on Stackoverflow
Solution 5 - IosAlbert RenshawView Answer on Stackoverflow
Solution 6 - IosfunclosureView Answer on Stackoverflow
Solution 7 - IoslukasView Answer on Stackoverflow
Solution 8 - IosMarcin KuptelView Answer on Stackoverflow
Solution 9 - IosjegorenkovView Answer on Stackoverflow
Solution 10 - IosVatsal ShuklaView Answer on Stackoverflow
Solution 11 - IosOscar ApelandView Answer on Stackoverflow
Solution 12 - IosAhmed SafadiView Answer on Stackoverflow
Solution 13 - IosAbhishek ThapliyalView Answer on Stackoverflow
Solution 14 - IosfredView Answer on Stackoverflow
Solution 15 - IospaulvsView Answer on Stackoverflow
Solution 16 - Iosdeepax11View Answer on Stackoverflow
Solution 17 - IosPhillip EnglishView Answer on Stackoverflow
Solution 18 - IosJFCaView Answer on Stackoverflow
Solution 19 - IosUtkarsh UpadhyayView Answer on Stackoverflow
Solution 20 - IosNicolai HarboView Answer on Stackoverflow
Solution 21 - IosMd Abul KashemView Answer on Stackoverflow