Skip to content

Instantly share code, notes, and snippets.

@Gujci
Last active November 16, 2022 17:42
Show Gist options
  • Save Gujci/201c76e67de95d17416295ef7e8f8386 to your computer and use it in GitHub Desktop.
Save Gujci/201c76e67de95d17416295ef7e8f8386 to your computer and use it in GitHub Desktop.
Simple snippet how to disable actions in a UITextField
import UIKit
@IBDesignable
class CustomTextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = true
@IBInspectable var isSelectEnabled: Bool = true
@IBInspectable var isSelectAllEnabled: Bool = true
@IBInspectable var isCopyEnabled: Bool = true
@IBInspectable var isCutEnabled: Bool = true
@IBInspectable var isDeleteEnabled: Bool = true
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
switch action {
case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
#selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
#selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
#selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
#selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
#selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
return false
default:
return super.canPerformAction(action, withSender: sender)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment