6

How disable Copy, Select All in UITextView, But i need link is clickable in UITextview.

Here is all action disable, But selection is allow. I need only clickable link.

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

        return false
}
rmaddy
  • 307,833
  • 40
  • 508
  • 550
Digvijaysinh Gida
  • 351
  • 1
  • 6
  • 18

2 Answers2

0

just hijack it,

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

swizzle() should be call once only.


How I know UITextSelectionView ?

Check View Hierarchy,

888


How I know method updateSelectionRects ?

via runtime,

import ObjectiveC ,

then,

        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) ?? " _ | _ ")
            }
        }
dengST30
  • 2,925
  • 18
  • 23
0

You can also hijack, like this

let hijackInvocation: @convention(block) (Unmanaged<NSObject>, AnyObject) -> Void = { objectRef, invocation in
    
}


func swizzle() {

    guard let cls = NSClassFromString("UITextLoupeInteraction") else { return }
    let originalSelector = NSSelectorFromString("loupeGesture:")
    let originMethod = class_getInstanceMethod(cls, originalSelector)
    if let originM = originMethod{
        let encode = method_getTypeEncoding(originM)
        class_replaceMethod(cls, originalSelector, imp_implementationWithBlock(hijackInvocation as Any), encode)
    }
}

swizzle() should be call just once.


what is UITextLoupeInteraction ?

999

from @dengST30's answer.

black_pearl
  • 2,033
  • 1
  • 18
  • 31