Home » Ios » Change of UITextField placeholder color

Change of UITextField placeholder color

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

No option in xib editor.

Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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"];

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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;

Questions:
Answers:

You can use the following code

[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

Questions:
Answers:

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
        }
    }
}

Questions:
Answers:

Try This.

UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];

Questions:
Answers:

@DogCoffee’s answer in Swift would be

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

textField.attributedPlaceholder = placeholder

Questions:
Answers:

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 ?? "")
        }
    }
}

}

Questions:
Answers:
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];