4

The UITextField's Select, SelectAll, Cut, Copy functionality is shown by default, When I long press or Double Tap on the TextField. I do not require this all features. Please tell me how to disable long press or double tap gesture functionality.

Pranit
  • 892
  • 12
  • 20
  • Please refer : http://stackoverflow.com/questions/6701019/how-to-disable-copy-paste-option-from-uitextfield-programmatically – byJeevan Mar 15 '17 at 06:23

2 Answers2

4

Following code will disable those options:

You have to subclass the UITextField and try to use this code to disable/hide caret and input (copy/paste)

override func caretRectForPosition(position: UITextPosition!) -> CGRect {
            return CGRect.zeroRect
        }

    override func selectionRectsForRange(range: UITextRange) -> [AnyObject] {
        return []
    }

    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        // Disable copy, select all, paste
        if action == Selector("copy:") || action == Selector("selectAll:") || action == Selector("paste:") {
            return false
        }
        // Default
        return super.canPerformAction(action, withSender: sender)
    }
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
3

Here is the swift 3.0 answer,

please try this,

override func canPerformAction(_ action: Selector, withSender sender: Any) -> Bool {
    UIMenuController.shared.menuVisible = false
    //do not display the menu
    self.resignFirstResponder()
    //do not allow the user to selected anything
    return false
}

Hope this will help you.

KAR
  • 3,303
  • 3
  • 27
  • 50