How to change UITextfield
placeholder
& fontsize
in SWIFT 2.0?
-
http://stackoverflow.com/a/26076202/988169 – pkc456 Oct 29 '15 at 12:51
-
2Possible duplicate of [Changing Placeholder Text Color with Swift](http://stackoverflow.com/questions/26076054/changing-placeholder-text-color-with-swift) – Eric Aya Oct 29 '15 at 12:55
11 Answers
#1. set Placeholder textfield color Programmatically
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle
OR
txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])
Note : Name
is your placeholder of textField
.
PlaceHolder TextFiled :
-------------------------------- OR -------------------------------------
#2. set Placeholder textfield color at runtime attribute

- 23,155
- 15
- 89
- 112
-
1The 2nd solution was best for me, I wanted to group all the fields in a `IBOutletCollection` however I was unsure how to define each UITextfields placeholder strings. This way works perfect for Swift 3. – Karl Taylor Jan 30 '17 at 13:57
-
15 years working on iOS and didn't know the second solution ! Perfect ! Thank you :) – Lapinou Jun 22 '18 at 06:53
-
1I was reading about the second solution and it could have some problem in production apparently because Apple can reject it – Patricio Vargas Sep 23 '18 at 19:14
-
7now (for swift 5, 13.4.1) it should be placeholderLabel.textColor instead of _placeholderLabel.textColor – Zaporozhchenko Oleksandr May 13 '20 at 19:16
Updated for Swift 3
If you want to change the UITextField Placeholder color for Swift 3, use the following lines of code:
let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])

- 9,564
- 146
- 81
- 122

- 3,209
- 26
- 29
Updated for Swift 5
For swift 5.0 use NSAttributedString.Key.foregroundColor instead of NSForegroundColorAttributeName
So, do it like so
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])

- 665
- 1
- 7
- 15
For swift 4 instead of
NSForegroundColorAttributeName
use
NSAttributedStringKey.foregroundColor

- 591
- 7
- 5
You can try with this sample code
let textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)

- 2,977
- 1
- 13
- 23
Placeholder for textfield Objective C
NSString* str = @"Placeholder text...";
NSRange range1 = [str rangeOfString:@"Placeholder text..."];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:str];
[attributedText setAttributes:@{
NSFontAttributeName:[UIFont fontWithName:customFont_NotoSans_Regular size:13.0]
}
range:range1];
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];
txtFld.font = [UIFont fontWithName:customFont_NotoSans_Regular size:13.0];
txtFld.keyboardType = UIKeyboardTypeDefault;
txtFld.attributedPlaceholder = attributedText;

- 4,422
- 1
- 27
- 33
It's easy to do with a subclass of UITextField.
Add placeholderColor
property to easily set the color, and then observer changing of .placeholder
to apply the color to it (with use of .attributedPlaceholder
property)
var placeholderColor: UIColor = .lightGray
override var placeholder: String? {
didSet {
let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
}
}
You do need to set the placeholder text programatically for the color to apply.

- 1,694
- 16
- 23
-
This is exactly what I was looking for! Subclassing and overriding default behavior. Thank you! :) – Vitya Shurapov Jan 06 '21 at 15:44
set Textfield placeholder
let leftBarButtonItem = UIBarButtonItem.init(image: UIImage(named:"ic_nav-bar_back.png"), landscapeImagePhone: nil, style: .plain, target: viewController, action: #selector(viewController.buttonClick(_:)))
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
leftBarButtonItem.tintColor = UIColor(hex: 0xED6E19)
viewController.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)

- 972
- 10
- 12
A simple solution is override placeholder
property in an UITextField
extension. It will update color of placeholder whole project. You don't need to update your code in many places.
extension UITextField {
var placeholder: String? {
get {
attributedPlaceholder?.string
}
set {
guard let newValue = newValue else {
attributedPlaceholder = nil
return
}
let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: Color.textFieldPlaceholder.color]
let attributedText = NSAttributedString(string: newValue, attributes: attributes)
attributedPlaceholder = attributedText
}
}
}

- 7,616
- 5
- 35
- 48
open your identity inspector by selecting text field and then put " placeholderLabel.textColor " in key path by pressing + button . Give the type " Color " and in value select desired RGB color.

- 1
Swift 5
textfiled.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Input Group Name", comment: "Input Group Name"), attributes: [NSAttributedString.Key.foregroundColor: yourColor.withAlphaComponent(0.5)])

- 1
- 1