Disable all UIResponderStandardEditActions when UITextfield is empty
I am currently trying to prevent all UIResponderStandardEditActions
like copy
, paste
, delete
from showing up when the UITextfield
is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12
or. I have tried overriding
the canPerformAction
method both in a UITextfield
extension
and using a custom class later assigned to the UITextfield
in the Storyboard
but no luck. Is there another way to do this. Here is what I have tried.
extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}
return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}
class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}
ios swift uitextfield uiresponder
add a comment |
I am currently trying to prevent all UIResponderStandardEditActions
like copy
, paste
, delete
from showing up when the UITextfield
is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12
or. I have tried overriding
the canPerformAction
method both in a UITextfield
extension
and using a custom class later assigned to the UITextfield
in the Storyboard
but no luck. Is there another way to do this. Here is what I have tried.
extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}
return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}
class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}
ios swift uitextfield uiresponder
add a comment |
I am currently trying to prevent all UIResponderStandardEditActions
like copy
, paste
, delete
from showing up when the UITextfield
is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12
or. I have tried overriding
the canPerformAction
method both in a UITextfield
extension
and using a custom class later assigned to the UITextfield
in the Storyboard
but no luck. Is there another way to do this. Here is what I have tried.
extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}
return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}
class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}
ios swift uitextfield uiresponder
I am currently trying to prevent all UIResponderStandardEditActions
like copy
, paste
, delete
from showing up when the UITextfield
is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12
or. I have tried overriding
the canPerformAction
method both in a UITextfield
extension
and using a custom class later assigned to the UITextfield
in the Storyboard
but no luck. Is there another way to do this. Here is what I have tried.
extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}
return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}
class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}
ios swift uitextfield uiresponder
ios swift uitextfield uiresponder
asked Nov 20 '18 at 9:41
mandem112mandem112
507
507
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool
or related condition to the switch case.
@IBDesignable
class ActionsDisabledUITextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = false
@IBInspectable var isSelectEnabled: Bool = false
@IBInspectable var isSelectAllEnabled: Bool = false
@IBInspectable var isCopyEnabled: Bool = false
@IBInspectable var isCutEnabled: Bool = false
@IBInspectable var isDeleteEnabled: Bool = false
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 true : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390122%2fdisable-all-uiresponderstandardeditactions-when-uitextfield-is-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool
or related condition to the switch case.
@IBDesignable
class ActionsDisabledUITextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = false
@IBInspectable var isSelectEnabled: Bool = false
@IBInspectable var isSelectAllEnabled: Bool = false
@IBInspectable var isCopyEnabled: Bool = false
@IBInspectable var isCutEnabled: Bool = false
@IBInspectable var isDeleteEnabled: Bool = false
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 true : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
add a comment |
Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool
or related condition to the switch case.
@IBDesignable
class ActionsDisabledUITextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = false
@IBInspectable var isSelectEnabled: Bool = false
@IBInspectable var isSelectAllEnabled: Bool = false
@IBInspectable var isCopyEnabled: Bool = false
@IBInspectable var isCutEnabled: Bool = false
@IBInspectable var isDeleteEnabled: Bool = false
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 true : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
add a comment |
Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool
or related condition to the switch case.
@IBDesignable
class ActionsDisabledUITextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = false
@IBInspectable var isSelectEnabled: Bool = false
@IBInspectable var isSelectAllEnabled: Bool = false
@IBInspectable var isCopyEnabled: Bool = false
@IBInspectable var isCutEnabled: Bool = false
@IBInspectable var isDeleteEnabled: Bool = false
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 true : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool
or related condition to the switch case.
@IBDesignable
class ActionsDisabledUITextField: UITextField {
@IBInspectable var isPasteEnabled: Bool = false
@IBInspectable var isSelectEnabled: Bool = false
@IBInspectable var isSelectAllEnabled: Bool = false
@IBInspectable var isCopyEnabled: Bool = false
@IBInspectable var isCutEnabled: Bool = false
@IBInspectable var isDeleteEnabled: Bool = false
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 true : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
answered Nov 20 '18 at 10:26
Emre ÖnderEmre Önder
834524
834524
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
add a comment |
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
This doesn't work mate
– mandem112
Nov 20 '18 at 10:56
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?
– Emre Önder
Nov 20 '18 at 11:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390122%2fdisable-all-uiresponderstandardeditactions-when-uitextfield-is-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown