swift textview禁止用户使用复制粘贴

时间:2024-01-23 10:30:11

//自定义一个TextView

class Own_TextView: UITextView {

    override func caretRect(for position: UITextPosition) -> CGRect {

        return CGRect.zero

    }

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

        if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) {

            return false

        }

        if action == #selector(paste(_:)){

            return false

        }

        return super.canPerformAction(action, withSender: sender)

    }

}

//禁止复制粘贴

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

       return false

 }

let textView = Own_TextView.init(frame: CGRect(x:5, y: NavigationBarHeight + 20, width: ScreenWidth - 10, height: screenHeight - NavigationBarHeight-398-20))