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
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,980评论 1 180
  • 亲爱的自己你辛苦了,在这14天的学习中的是最棒的,不管在学习的过程中有多少困难和辛苦,哪怕是每天只休息四五小时...
    谢金花阅读 2,839评论 0 2
  • 《七绝*叹春》 文*瀚海望客 人言四月芳菲尽, 满地残红伴绿茵。 花谢花开花间事, 草枯草翠草辞春。
    瀚海望客阅读 1,612评论 0 0
  • 2016年6月29号,我毕业了,其实前天的前一天就拿到毕业证的我就算毕业了,但是由于我的各种阿Q和不舍,甚至鸵鸟的...
    墨雪竹阅读 2,929评论 0 0
  • 将来的理财计划还没完全想好,就说说一下这些年的理财经历。 从一开始只知道把钱存银行,根据自己经济情况和实际用钱的情...
    烟雨程程阅读 1,256评论 1 2

友情链接更多精彩内容