如何使用Swift 4.2禁用UITextView中的“复制,全选”?

Dig*_*jay 6 uitextview ios swift

如何禁用“复制”,“全选” UITextView,但我需要在中单击链接UITextview

这是所有动作禁用,但允许选择。我只需要可点击的链接。

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

        return false
}
Run Code Online (Sandbox Code Playgroud)

den*_*T30 0

只是hijack它,

func swizzle() {
    guard let cls = NSClassFromString("UITextSelectionView") else { return }
    let originalSelector = NSSelectorFromString("updateSelectionRects")
    let swizzledSelector = #selector(UIView.updateSelectionRectsHijack)
    let originMethod = class_getInstanceMethod(cls, originalSelector)
    let swizzleMethod = class_getInstanceMethod(UIView.self, swizzledSelector)
    if let swizzledMethod = swizzleMethod, let originalMethod = originMethod{
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

extension UIView{
    @objc func updateSelectionRectsHijack(){ }
}
Run Code Online (Sandbox Code Playgroud)

swizzle()应该只调用一次。


我怎么知道UITextSelectionView

查看View Hierarchy

第888章


我怎么知道方法updateSelectionRects

通过runtime

import ObjectiveC,

然后,

        var count: UInt32 = 0
        guard let methodArr = class_copyMethodList(NSClassFromString("UITextSelectionView"), &count) else { return }
        
        let cnt = Int(count)
        for i in 0..<cnt{
            let method = methodArr[i]
            let name = method_getName(method)
            if let type = method_getTypeEncoding(method){
                print(name, String(utf8String: type) ?? " _ | _ ")
            }
        }
Run Code Online (Sandbox Code Playgroud)