更改UITextField占位符颜色

如何dynamic更改UITextField占位符颜色? 这总是一样的系统颜色。

在xib编辑器中没有选项。

从文档

@属性(非primefaces,复制)NSAttributedString *属性Placeholder

该属性默认为零。 如果设置,则占位符string将使用70%的灰色颜色和属性string的其余样式信息(文本颜色除外)绘制。 为该属性分配一个新的值也可以用相同的string数据replace占位符属性的值,尽pipe没有任何格式信息。 为该属性分配新的值不会影响文本字段的其他任何与样式相关的属性。

Objective-C的

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

迅速

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

简单而完美的解决方案

 _placeholderLabel.textColor 

迅速

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

PS从Stackoverflow复制3个不同的答案。

使用下面的代码

 [YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"]; 

我在SWIFT中使用这个:

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

看起来这是为别人工作…我不知道为什么它之前没有为我工作…也许一些项目设置。 感谢您的意见。 目前我没有办法再试一次。

已过时: 但我不知道为什么,文本应用正确,但占位符颜色保持不变(黑色/灰色)。

–iOS8

首先添加这个扩展名

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

然后,您可以通过情节提要改变占位符的文字颜色,或者像这样设置:

 textfield.placeHolderTextColor = UIColor.red 

尝试这个:

 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; 

您可以使用下面的代码

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

这个解决scheme没有任何子类,没有任何私人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 } } } 

尝试这个。

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

@ DogCoffee在Swift中的回答是

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

这是@Medin Piranej上面提供的扩展的改进版本(顺便说一句好主意!)。 如果您尝试获取placeHolderTextColor,则此版本可避免无限循环,并在颜色集为零时防止崩溃。

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

}

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