Xcode 9 UITextView links no longer clickable

后端 未结 2 1721
轮回少年
轮回少年 2021-01-02 07:31

Prior to Xcode 9 and iOS 11 I had a UITextView within a UITableViewCell that contained multiple links. Each link worked as expected, however since

2条回答
  •  隐瞒了意图╮
    2021-01-02 08:11

    Specifically in iOS 11.0 and 11.1 (not later in 11.2+, not earlier in 10.x), textView(_:shouldInteractWith:in:interaction) from UITextViewDelegate is called from an interaction with a UILongPressGestureRecognizer instead of a UITapGestureRecognizer.

    For those two iOS versions, user needs a small delay long press instead of a tap for a native interaction with UITextView links.

    If the callback doesn't get called at all for those two iOS versions, even on a long press, you've likely been messing with gesture recognizers by subclassing your UITextView and overriding gestureRecognizerShouldBegin(_) to return false when it shouldn't.

    Here is an example of quick partial workaround for gestureRecognizerShouldBegin(_) that will disable loupe/magnifier long press (if that's the desired intent of the override), but still allow long press on links:

    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 {
            // allowing taps for links
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer,
            // allowing small delay long press for links (required for iOS 11.0-11.1)
            // average comparison value is used to distinguish between:
            // 0.12 (smallDelayRecognizer)
            // 0.5 (textSelectionForce and textLoupe)
            longPressGestureRecognizer.minimumPressDuration < 0.325 {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        gestureRecognizer.isEnabled = false
        return false
    }
    

    An alternative is to fully disallow both UILongPressGestureRecognizer and UITapGestureRecognizer except for a self-made UITapGestureRecognizer that you would have build yourself to interact with links.

提交回复
热议问题