Clear UITextField Placeholder text on tap
In Xcode4 I've created some placeholder text for a UITextField and I'd like it to clear when the user taps in the box.
So, in the Attributes Inspector for the text field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).
Is there any way of removing the placeholder text immediately on tapping in the text box?
iphone uitextfield
add a comment |
In Xcode4 I've created some placeholder text for a UITextField and I'd like it to clear when the user taps in the box.
So, in the Attributes Inspector for the text field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).
Is there any way of removing the placeholder text immediately on tapping in the text box?
iphone uitextfield
add a comment |
In Xcode4 I've created some placeholder text for a UITextField and I'd like it to clear when the user taps in the box.
So, in the Attributes Inspector for the text field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).
Is there any way of removing the placeholder text immediately on tapping in the text box?
iphone uitextfield
In Xcode4 I've created some placeholder text for a UITextField and I'd like it to clear when the user taps in the box.
So, in the Attributes Inspector for the text field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).
Is there any way of removing the placeholder text immediately on tapping in the text box?
iphone uitextfield
iphone uitextfield
asked Dec 16 '11 at 10:31
Snowcrash
37.1k39132212
37.1k39132212
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
make your ViewController the delegate of the textField and implement those two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.placeholder = @"Your Placeholdertext";
}
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
|
show 1 more comment
The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField
to worry about? Now you have to identify which UITextField
is referred to in textFieldDidEndEditing:textField
(possibly by use of the tag property), and that results in more unnecessary code and logic.
A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField
doesn't have to identify the textField
to set back its corresponding text after it was nullified as in Bauch's solution.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
}
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
add a comment |
You should also check if text filed is empty then you should put place holder again
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
{
[textField setText:@""];
textField.placeholder = @"Your Placeholdertext";
}
}
add a comment |
use this..
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
don't forget to add the delegate for the textfield to your file Owner.
add a comment |
If you have more than one TextField
1) Add String variable to your class
class YourViewController : UIViewController {
var placeHolder = ""
2) Add UITextFieldDelegate
extension YourViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
placeHolder = textField.placeholder ?? ""
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.placeholder == ""{
textField.placeholder = placeHolder
}
}
add a comment |
In your .h file declare a function like
-(IBAction)clear:(id)sender;
attach this function to your touchdown event of your UITextField
.
in your .m file
-(IBAction)clear:(id)sender
{
myplaceHolderText.text=@"";
}
add a comment |
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
textfield delegate make place holder value to nil
add a comment |
The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again.
If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:
class CustomTextField: UITextField, UITextFieldDelegate {
private func setup() {
//do additional setup like attributedPlaceholder, inset, etc.
self.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var emailTextField: CustomTextField!
@IBOutlet weak var passwordTextField: CustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
textFields = [emailTextField, passwordTextField]
for textField in textFields {
textField.delegate = self
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
add a comment |
In case of SWIFT 3 or later
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = nil
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = "Text Placeholder"
}
add a comment |
On Button action Event put this Code:
txtName.placeholder = @"";
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%2f8532874%2fclear-uitextfield-placeholder-text-on-tap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
make your ViewController the delegate of the textField and implement those two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.placeholder = @"Your Placeholdertext";
}
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
|
show 1 more comment
make your ViewController the delegate of the textField and implement those two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.placeholder = @"Your Placeholdertext";
}
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
|
show 1 more comment
make your ViewController the delegate of the textField and implement those two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.placeholder = @"Your Placeholdertext";
}
make your ViewController the delegate of the textField and implement those two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.placeholder = @"Your Placeholdertext";
}
answered Dec 16 '11 at 10:37
Matthias Bauch
83k15205242
83k15205242
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
|
show 1 more comment
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
In the second method you have to check that the textfield has empty value or not?
– KingofBliss
Dec 16 '11 at 10:43
4
4
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
nope. The placeholder text just isn't visible if there is "regular" text.
– Matthias Bauch
Dec 16 '11 at 10:46
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
Oh.. Yeah i forget.. :) Thanks for the info..
– KingofBliss
Dec 16 '11 at 10:47
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
i have add some code into my answer to check is text field is empty or not
– Maulik
Dec 16 '11 at 10:57
2
2
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
– zhangyoufu
Sep 20 '13 at 10:47
|
show 1 more comment
The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField
to worry about? Now you have to identify which UITextField
is referred to in textFieldDidEndEditing:textField
(possibly by use of the tag property), and that results in more unnecessary code and logic.
A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField
doesn't have to identify the textField
to set back its corresponding text after it was nullified as in Bauch's solution.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
}
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
add a comment |
The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField
to worry about? Now you have to identify which UITextField
is referred to in textFieldDidEndEditing:textField
(possibly by use of the tag property), and that results in more unnecessary code and logic.
A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField
doesn't have to identify the textField
to set back its corresponding text after it was nullified as in Bauch's solution.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
}
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
add a comment |
The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField
to worry about? Now you have to identify which UITextField
is referred to in textFieldDidEndEditing:textField
(possibly by use of the tag property), and that results in more unnecessary code and logic.
A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField
doesn't have to identify the textField
to set back its corresponding text after it was nullified as in Bauch's solution.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
}
The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField
to worry about? Now you have to identify which UITextField
is referred to in textFieldDidEndEditing:textField
(possibly by use of the tag property), and that results in more unnecessary code and logic.
A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField
doesn't have to identify the textField
to set back its corresponding text after it was nullified as in Bauch's solution.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
}
edited Nov 23 '16 at 10:58
Meet Doshi
2,65782766
2,65782766
answered Feb 6 '14 at 9:04
etayluz
7,58265584
7,58265584
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
add a comment |
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
This could cause app store rejection for using private API
– Hellojeffy
Sep 11 '16 at 17:30
add a comment |
You should also check if text filed is empty then you should put place holder again
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
{
[textField setText:@""];
textField.placeholder = @"Your Placeholdertext";
}
}
add a comment |
You should also check if text filed is empty then you should put place holder again
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
{
[textField setText:@""];
textField.placeholder = @"Your Placeholdertext";
}
}
add a comment |
You should also check if text filed is empty then you should put place holder again
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
{
[textField setText:@""];
textField.placeholder = @"Your Placeholdertext";
}
}
You should also check if text filed is empty then you should put place holder again
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
{
[textField setText:@""];
textField.placeholder = @"Your Placeholdertext";
}
}
edited Dec 20 '11 at 4:26
answered Dec 16 '11 at 10:57
Maulik
18.1k1473130
18.1k1473130
add a comment |
add a comment |
use this..
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
don't forget to add the delegate for the textfield to your file Owner.
add a comment |
use this..
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
don't forget to add the delegate for the textfield to your file Owner.
add a comment |
use this..
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
don't forget to add the delegate for the textfield to your file Owner.
use this..
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
don't forget to add the delegate for the textfield to your file Owner.
answered Dec 16 '11 at 10:36
Ankit Srivastava
9,754650104
9,754650104
add a comment |
add a comment |
If you have more than one TextField
1) Add String variable to your class
class YourViewController : UIViewController {
var placeHolder = ""
2) Add UITextFieldDelegate
extension YourViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
placeHolder = textField.placeholder ?? ""
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.placeholder == ""{
textField.placeholder = placeHolder
}
}
add a comment |
If you have more than one TextField
1) Add String variable to your class
class YourViewController : UIViewController {
var placeHolder = ""
2) Add UITextFieldDelegate
extension YourViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
placeHolder = textField.placeholder ?? ""
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.placeholder == ""{
textField.placeholder = placeHolder
}
}
add a comment |
If you have more than one TextField
1) Add String variable to your class
class YourViewController : UIViewController {
var placeHolder = ""
2) Add UITextFieldDelegate
extension YourViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
placeHolder = textField.placeholder ?? ""
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.placeholder == ""{
textField.placeholder = placeHolder
}
}
If you have more than one TextField
1) Add String variable to your class
class YourViewController : UIViewController {
var placeHolder = ""
2) Add UITextFieldDelegate
extension YourViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
placeHolder = textField.placeholder ?? ""
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.placeholder == ""{
textField.placeholder = placeHolder
}
}
answered Nov 12 at 9:59
Ammar
1828
1828
add a comment |
add a comment |
In your .h file declare a function like
-(IBAction)clear:(id)sender;
attach this function to your touchdown event of your UITextField
.
in your .m file
-(IBAction)clear:(id)sender
{
myplaceHolderText.text=@"";
}
add a comment |
In your .h file declare a function like
-(IBAction)clear:(id)sender;
attach this function to your touchdown event of your UITextField
.
in your .m file
-(IBAction)clear:(id)sender
{
myplaceHolderText.text=@"";
}
add a comment |
In your .h file declare a function like
-(IBAction)clear:(id)sender;
attach this function to your touchdown event of your UITextField
.
in your .m file
-(IBAction)clear:(id)sender
{
myplaceHolderText.text=@"";
}
In your .h file declare a function like
-(IBAction)clear:(id)sender;
attach this function to your touchdown event of your UITextField
.
in your .m file
-(IBAction)clear:(id)sender
{
myplaceHolderText.text=@"";
}
answered Dec 16 '11 at 10:48
Ajeet Pratap Maurya
3,64432443
3,64432443
add a comment |
add a comment |
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
textfield delegate make place holder value to nil
add a comment |
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
textfield delegate make place holder value to nil
add a comment |
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
textfield delegate make place holder value to nil
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.placeholder=nil;
}
textfield delegate make place holder value to nil
edited Apr 8 '16 at 11:37
Maciek z Wrocławia
4,69213040
4,69213040
answered Aug 6 '14 at 6:04
chaithraVeeresh
227310
227310
add a comment |
add a comment |
The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again.
If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:
class CustomTextField: UITextField, UITextFieldDelegate {
private func setup() {
//do additional setup like attributedPlaceholder, inset, etc.
self.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var emailTextField: CustomTextField!
@IBOutlet weak var passwordTextField: CustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
textFields = [emailTextField, passwordTextField]
for textField in textFields {
textField.delegate = self
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
add a comment |
The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again.
If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:
class CustomTextField: UITextField, UITextFieldDelegate {
private func setup() {
//do additional setup like attributedPlaceholder, inset, etc.
self.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var emailTextField: CustomTextField!
@IBOutlet weak var passwordTextField: CustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
textFields = [emailTextField, passwordTextField]
for textField in textFields {
textField.delegate = self
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
add a comment |
The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again.
If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:
class CustomTextField: UITextField, UITextFieldDelegate {
private func setup() {
//do additional setup like attributedPlaceholder, inset, etc.
self.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var emailTextField: CustomTextField!
@IBOutlet weak var passwordTextField: CustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
textFields = [emailTextField, passwordTextField]
for textField in textFields {
textField.delegate = self
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again.
If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:
class CustomTextField: UITextField, UITextFieldDelegate {
private func setup() {
//do additional setup like attributedPlaceholder, inset, etc.
self.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var emailTextField: CustomTextField!
@IBOutlet weak var passwordTextField: CustomTextField!
override func viewDidLoad() {
super.viewDidLoad()
textFields = [emailTextField, passwordTextField]
for textField in textFields {
textField.delegate = self
}
// MARK: UITextFieldDelegate methods
func textFieldDidBeginEditing(textField: UITextField) {
textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
}
func textFieldDidEndEditing(textField: UITextField) {
textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
}
}
edited Jul 12 '16 at 11:40
answered Jul 12 '16 at 10:54
Yurii Koval
219314
219314
add a comment |
add a comment |
In case of SWIFT 3 or later
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = nil
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = "Text Placeholder"
}
add a comment |
In case of SWIFT 3 or later
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = nil
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = "Text Placeholder"
}
add a comment |
In case of SWIFT 3 or later
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = nil
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = "Text Placeholder"
}
In case of SWIFT 3 or later
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = nil
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = "Text Placeholder"
}
answered Oct 31 '17 at 10:34
Ghulam Rasool
2,09911326
2,09911326
add a comment |
add a comment |
On Button action Event put this Code:
txtName.placeholder = @"";
add a comment |
On Button action Event put this Code:
txtName.placeholder = @"";
add a comment |
On Button action Event put this Code:
txtName.placeholder = @"";
On Button action Event put this Code:
txtName.placeholder = @"";
answered Dec 16 '11 at 10:37
Developer
457316
457316
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f8532874%2fclear-uitextfield-placeholder-text-on-tap%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