UITextView 设置不允许选中,允许链接跳转

UITextView html富文本 设置不允许选中,允许链接跳转

UITextView: Disable selection, allow links

方案一

注意:此方案会弹出 放大镜

class NewTextView: UITextView {
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if #available(iOS 10, *) {
            if action == #selector(UIResponderStandardEditActions.paste(_:))
                || action == #selector(UIResponderStandardEditActions.select(_:))
                || action == #selector(UIResponderStandardEditActions.selectAll(_:))
                || action == #selector(UIResponderStandardEditActions.copy(_:))
                || action == #selector(UIResponderStandardEditActions.cut(_:))
                || action == #selector(UIResponderStandardEditActions.delete(_:))
            {
                OperationQueue.main.addOperation {
                    UIMenuController.shared.setMenuVisible(false, animated: false)
                }
                self.selectedRange = NSRange.init(location: 0, length: 0)

                return false
            }
        } else {
            if action == #selector(paste(_:))
                || action == #selector(select(_:))
                || action == #selector(selectAll(_:))
                || action == #selector(copy(_:))
                || action == #selector(cut(_:))
                || action == #selector(delete(_:))
            {
                OperationQueue.main.addOperation {
                    UIMenuController.shared.setMenuVisible(false, animated: false)
                }
                return false
            }
        }
        
        //return true : this is not correct
        return super.canPerformAction(action, withSender: sender)
    }
}

方案二


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)
            }
        }
    }
    
}

方案三

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
    }
}

你可能感兴趣的:(UITextView 设置不允许选中,允许链接跳转)