76

How to dynamically change placeholder color of the UITextField? This is always the same system color.

No option in xib editor.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
theWalker
  • 2,022
  • 2
  • 18
  • 27

13 Answers13

165

From Docs

@property(nonatomic, copy) NSAttributedString *attributedPlaceholder

This property is nil by default. If set, the placeholder string is drawn using a 70% grey color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.

Objective-C

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }];
self.myTextField.attributedPlaceholder = str;

Swift

let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
myTextField.attributedPlaceholder = str

Swift 4

let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • I can't test it, NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:myString]; is always nil. – theWalker Jan 12 '14 at 14:10
  • Why are you creating a mutable attributed string? Just do: `NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ UITextAttributeTextColor : [UIColor redColor] }];` – rmaddy Jan 12 '14 at 17:29
  • Cheers @rmaddy, i used NSForegroundColorAttributeName. – DogCoffee Jan 13 '14 at 00:59
41

Easy and perfect solution.

_placeholderLabel.textColor

In swift

myTextField.attributedPlaceholder = 
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])

Objective-C

UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
   [[NSAttributedString alloc]
   initWithString:@"Full Name"
   attributes:@{NSForegroundColorAttributeName:color}];

P.S Copied 3 different answers from Stackoverflow.

iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • 3
    Warning for `_placeholderLabel`: some users report App Store rejection: http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color#comment56758204_22017127 – ftvs Apr 08 '16 at 07:58
  • If you used _placeholderLabel, you will get this crash Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug – Hosny Dec 16 '19 at 19:07
12

Use below code

[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
  • 4
    Your app will be rejected if you use this, you are accessing the private API. Instead use the attributedPlaceholder property of the UITextField. – aumanets Apr 04 '14 at 11:10
  • i think this is not private API you can use in current app also. Not using any private api – Pradhyuman sinh Apr 04 '14 at 11:14
  • It is private API, you are accessing the private variable "_placeholderLabel". It wouldn't be private if you could do something like self.placeholderLabel. – aumanets Apr 06 '14 at 11:00
  • 1
    @aumanets it's not private api... It's private property of UITextfield.. It's key value coding... check this http://stackoverflow.com/questions/16601468/how-change-placeholder-text-color-ios – Pradhyuman sinh Apr 07 '14 at 04:32
  • 2
    Yes you are right, it's a private property. Anyway it's not a good practice accessing properties/variables this way. One day Apple can change the name of that property to something else which will crash the app, and you will not get any compile warning in your code. – aumanets Apr 07 '14 at 12:55
  • use of attributed strings is preferred – Ron Aug 26 '15 at 19:40
  • 1
    If you used _placeholderLabel, you will get this crash Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug – Hosny Dec 16 '19 at 19:07
5

First add this extension

extension UITextField{
    @IBInspectable var placeHolderTextColor: UIColor? {
        set {
            let placeholderText = self.placeholder != nil ? self.placeholder! : ""
            attributedPlaceholder = NSAttributedString(string:placeholderText, attributes:[NSForegroundColorAttributeName: newValue!])
        }
        get{
            return self.placeHolderTextColor
        }
    }
}

Then you can change placeholder text color via storyboard or by just setting it like this :

textfield.placeHolderTextColor = UIColor.red
Medin Piranej
  • 789
  • 1
  • 8
  • 10
3

I use this in SWIFT:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])

It seems that this works for others... I have no idea why it haven't worked for me before... maybe some project settings. Thanks for the comments. Currently I have no way how to test it again.

Obsolete: But I don't know why, text is applied correctly, but placeholder color remains same (black/gray).

--iOS8

Tomino
  • 5,969
  • 6
  • 38
  • 50
2

Try this:

NSAttributedString *strUser = [[NSAttributedString alloc] initWithString:@"Username" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];

self.username.attributedPlaceholder = strUser;
self.password.attributedPlaceholder = strPassword;
Dan Fairaizl
  • 2,172
  • 2
  • 28
  • 31
1

You can use the following code

[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
Hardik Mamtora
  • 1,642
  • 17
  • 23
1

This solution works without any subclassing and without any private ivars:

@IBOutlet weak var emailTextField: UITextField! {
    didSet {
        if emailTextField != nil {
            let placeholderText = NSLocalizedString("Tap here to enter", comment: "Tap here to enter")
            let placeholderString = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor(white: 0.66, alpha: 1.0)])
            emailTextField.attributedPlaceholder = placeholderString
        }
    }
}
Ron
  • 1,610
  • 15
  • 23
1

Try This.

UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
1

Swift 4

let placeholderColor = UIColor.red
self.passwordTextField?.attributedPlaceholder = 
NSAttributedString(string:"placeholderText", attributes: 
[NSAttributedStringKey.foregroundColor : placeholderColor])
Adriana
  • 225
  • 3
  • 8
0

@DogCoffee's answer in Swift would be

let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)

textField.attributedPlaceholder = placeholder
rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84
0

This is an improved version of the extension provided by @Medin Piranej above (good idea by the way!). This version avoids an endless cycle if you try to get the placeHolderTextColor and prevents crashes if the color set is nil.

public extension UITextField {

@IBInspectable public var placeholderColor: UIColor? {
    get {
        if let attributedPlaceholder = attributedPlaceholder, attributedPlaceholder.length > 0 {
            var attributes = attributedPlaceholder.attributes(at: 0,
                                                              longestEffectiveRange: nil,
                                                              in: NSRange(location: 0, length: attributedPlaceholder.length))
            return attributes[NSForegroundColorAttributeName] as? UIColor
        }
        return nil
    }
    set {
        if let placeholderColor = newValue {
            attributedPlaceholder = NSAttributedString(string: placeholder ?? "",
                                                       attributes:[NSForegroundColorAttributeName: placeholderColor])
        } else {
            // The placeholder string is drawn using a system-defined color.
            attributedPlaceholder = NSAttributedString(string: placeholder ?? "")
        }
    }
}

}

boherna
  • 659
  • 8
  • 7
0

for swift 3 ,we can use this code for change the placeholder text color for UITextfield

 let placeholderColor = UIColor.red
 mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])
Lalit Pratap
  • 119
  • 1
  • 11