UITextView link is selected without the ability to select text

I am trying to get a setting similar to that used by Facebook (if they use UITextView). I want the links to be detected automatically, but I don’t want any other text to UITextViewbe selectable. Thus, the user can click on the link, but cannot select any other text.

Despite the search around, I have yet to find a solution, since selecting links for its operation requires that the entire text view can be selected.

+6
source share
7 answers

This answer is for iOS 10.3.x and below, where your UIView is not embedded in the subview. For a more reliable, up-to-date answer, see the Cœur answer below .

You must be prevented from UITextViewbecoming the first respondent.

1. Subclass UITextViewfor your own custom class ( MyTextView).

2. Override canBecomeFirstResponder(). Here is an example in Swift:

Swift 3:

class MyTextView: UITextView {
    override func becomeFirstResponder() -> Bool {
        return false
    }
}

Swift 2:

class MyTextView: UITextView {
    override func canBecomeFirstResponder() -> Bool {
        return false
    }
}

All links found will continue to be included. I checked this with a phone number.

0
source

You need to subclass UITextViewand override the method gestureRecognizerShouldBegin (_:)as follows:

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if isEditable == false {
        if let gesture =  gestureRecognizer as? UILongPressGestureRecognizer, gesture.minimumPressDuration == 0.5 {
            return false
        }
    }
    return true
}

this will prevent text selection, but the link will work as expected

: , . , ( UITapGesture "minimalNumberOfTaps", ), ( 0,7 ) :

var lastTapTime: TimeInterval = 0
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if isEditable == false {
            if let gesture =  gestureRecognizer as? UILongPressGestureRecognizer, gesture.minimumPressDuration == 0.5 {
        return false
            }
        }
        if Date().timeIntervalSince1970 >= lastTapTime + 0.7 {
            lastTapTime = Date().timeIntervalSince1970
            return true
        } else {
            return false
        }
    }

, , , 🤷♂️

+3

- iOS 11.2

, UITextView UITextView , - .

:

  • isEditable
  • isScrollEnabled
/// Class to allow links but no selection.
/// Basically, it disables unwanted UIGestureRecognizer from UITextView.
/// https://stackoverflow.com/a/49428307/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
        // /questions/725485/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
    }
}

- iOS 11.1

UITextView iOS 11.0-11.1 : Xcode 9 UITextView

, , UITextView UITextView , - UITextView -.

:

  • isScrollEnabled
  • iOS 11.0 iOS 11.1,
/// Class to support links and to disallow selection.
/// It disables most UIGestureRecognizer from UITextView and adds a UITapGestureRecognizer.
/// https://stackoverflow.com/a/49428307/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:
        // /questions/725485/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: characterIndex, 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)
            }
        }
    }
}
+3

UITextView UITextView selectedTextRange, nil. - , ( , ).

class CustomTextView: UITextView {
override public var selectedTextRange: UITextRange? {
    get {
        return nil
    }
    set { }
}
+3

, UIGestureRecognizer s.

, point(inside:with:) , : fooobar.com/questions/556919/...

+2

, ;

class LinkDetectingTextView: UITextView {
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if isEditable == false {
            if let _ = gestureRecognizer as? UITapGestureRecognizer {
                return false
            }

            if let longPressRecognizer = gestureRecognizer as? UILongPressGestureRecognizer,
                longPressRecognizer.minimumPressDuration == 0.5 { // prevent to select text but allow certain functionality in application

                return false
            }
        }

        return true
    }
}

Also, set minimumPressDuration for longPressGestureRecognizer in your application to a value other than 0.5.

0
source

Xamarin.iOS:

  1. Create custom UITextView
  2. Override the method GestureRecognizerShouldBeginin yourUITextView
public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer is UILongPressGestureRecognizer ||
        gestureRecognizer.Name != "UITextInteractionNameLinkTap")
    {
        return false;
    }
    return true;
}
0
source

All Articles