Last active
November 16, 2022 17:42
-
-
Save Gujci/201c76e67de95d17416295ef7e8f8386 to your computer and use it in GitHub Desktop.
Simple snippet how to disable actions in a UITextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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