[英]Change of UITextField placeholder color
How to dynamically change placeholder color of the UITextField
? 如何动态更改
UITextField
占位符颜色? This is always the same system color. 这始终是相同的系统颜色。
No option in xib editor. xib编辑器中没有选项。
From Docs 来自Docs
@property(nonatomic, copy) NSAttributedString *attributedPlaceholder
@property(非原子,复制)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.
如果设置,则使用70%灰色和属性字符串的剩余样式信息(文本颜色除外)绘制占位符字符串。 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 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 斯威夫特4
let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
_placeholderLabel.textColor
In swift 在迅速
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])
Objective-C Objective-C的
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
PS Copied 3 different answers from Stackoverflow. 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"];
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
I use this in SWIFT: 我在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 --iOS8
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;
您可以使用以下代码
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
This solution works without any subclassing and without any private ivars: 此解决方案在没有任何子类化且没有任何私有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
}
}
}
Try This. 试试这个。
UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];
@DogCoffee's answer in Swift would be @ DogCoffee在Swift中的回答是
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
This is an improved version of the extension provided by @Medin Piranej above (good idea by the way!). 这是@Medin Piranej上面提供的扩展的改进版本(顺便说一句好主意!)。 This version avoids an endless cycle if you try to get the placeHolderTextColor and prevents crashes if the color set is nil.
如果您尝试获取placeHolderTextColor并且如果颜色集为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 ?? "")
}
}
}
} }
for swift 3 ,we can use this code for change the placeholder text color for UITextfield 对于swift 3,我们可以使用此代码更改UITextfield的占位符文本颜色
let placeholderColor = UIColor.red
mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])
Swift 4 斯威夫特4
let placeholderColor = UIColor.red
self.passwordTextField?.attributedPlaceholder =
NSAttributedString(string:"placeholderText", attributes:
[NSAttributedStringKey.foregroundColor : placeholderColor])
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.