我想改变我在UITextField
控件中设置的占位文本的颜色,让它变成黑色。
我希望能够不使用常规文本作为占位符,并且无需覆盖所有方法来模仿占位符的行为。
我相信如果我覆盖这个方法:
- (void)drawPlaceholderInRect:(CGRect)rect
那么我应该能够这样做。但我不确定如何从这个方法中访问实际的占位符对象。
我想改变我在UITextField
控件中设置的占位文本的颜色,让它变成黑色。
我希望能够不使用常规文本作为占位符,并且无需覆盖所有方法来模仿占位符的行为。
我相信如果我覆盖这个方法:
- (void)drawPlaceholderInRect:(CGRect)rect
那么我应该能够这样做。但我不确定如何从这个方法中访问实际的占位符对象。
自从iOS 6引入了UIView中的属性字符串,就可以像这样为占位符文本分配颜色:
if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
} else {
NSLog(@"Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0");
// TODO: Add fall-back code to set placeholder color.
}
易于操作且无痛苦,可能是某些人的简便选择。
_placeholderLabel.textColor
不建议在生产环境中使用,因为苹果可能会拒绝您的提交。
_placeholderLabel
是一个私有属性。使用私有 API 可能会导致该解决方案被拒绝。 - Sean Kladek你可以通过如下方式覆盖 drawPlaceholderInRect:(CGRect)rect
方法来手动渲染占位符文本:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
这样的写法可能更好。这样可以尊重 textAlignment
属性。我已经将其添加到我的 SSTextField 类中,欢迎在您的项目中使用。 - Sam Soffes在Swift <3.0中可行:
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
iOS 8.2和iOS 8.3 beta 4中测试通过。
Swift 3:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName : UIColor.red])
Swift 4:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
Swift 4.2:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
您可以使用以下代码将占位符文本颜色更改为任何颜色。
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
也许你想尝试这种方法,但是苹果可能会警告你访问私有的ivar:
[self.myTextField setValue:[UIColor darkGrayColor]
forKeyPath:@"_placeholderLabel.textColor"];
注意
根据Martin Alléus的说法,这在iOS 7上不再起作用。
访问UITextField的_placeholderLabel ivar是被禁止的。这是一个应用程序错误。
- Ryder Mackay为了在Storyboard中更改占位符的颜色,请创建一个扩展并使用以下代码(如果您认为可以使代码更清晰和安全,请随意更新此代码)。
extension UITextField {
@IBInspectable var placeholderColor: UIColor {
get {
guard let currentAttributedPlaceholderColor = attributedPlaceholder?.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as? UIColor else { return UIColor.clear }
return currentAttributedPlaceholderColor
}
set {
guard let currentAttributedString = attributedPlaceholder else { return }
let attributes = [NSForegroundColorAttributeName : newValue]
attributedPlaceholder = NSAttributedString(string: currentAttributedString.string, attributes: attributes)
}
}
}
extension UITextField {
@IBInspectable var placeholderColor: UIColor {
get {
return attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor ?? .clear
}
set {
guard let attributedPlaceholder = attributedPlaceholder else { return }
let attributes: [NSAttributedStringKey: UIColor] = [.foregroundColor: newValue]
self.attributedPlaceholder = NSAttributedString(string: attributedPlaceholder.string, attributes: attributes)
}
}
}
extension UITextField {
@IBInspectable var placeholderColor: UIColor {
get {
return attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor ?? .clear
}
set {
guard let attributedPlaceholder = attributedPlaceholder else { return }
let attributes: [NSAttributedString.Key: UIColor] = [.foregroundColor: newValue]
self.attributedPlaceholder = NSAttributedString(string: attributedPlaceholder.string, attributes: attributes)
}
}
}
NSAttributedStringKey
。 - Milan Kamilya在 Swift 中:
if let placeholder = yourTextField.placeholder {
yourTextField.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSForegroundColorAttributeName: UIColor.blackColor()])
}
在 Swift 4.0 中:
if let placeholder = yourTextField.placeholder {
yourTextField.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])
}
以下仅适用于iOS6+(如Alexander W的评论中所示):
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
我已经遇到过这个问题。在我的情况下,以下代码是正确的。
Objective C
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
适用于 Swift 4.X
tf_mobile.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
针对iOS 13的Swift代码
tf_mobile.attributedPlaceholder = NSAttributedString(string:"PlaceHolder Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
您也可以使用以下代码适用于iOS 13
let iVar = class_getInstanceVariable(UITextField.self, "_placeholderLabel")!
let placeholderLabel = object_getIvar(tf_mobile, iVar) as! UILabel
placeholderLabel.textColor = .red
希望这能对你有所帮助。
viewWillAppear
或viewDidAppear
中进行这个更改。viewDidLoad
太早了。 - FourUIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 0.3)
构造函数来代替UIColor(red: 255, green: 255, blue: 255, alpha: 0.3)
。 - AppreciateIt
attributedPlaceholder
的文档表明它使用文本属性,除了颜色。 - Matt Connolly[[NSAttributedString alloc] initWithString:@"placeholder" attributes: @{NSForegroundColorAttributeName: color, NSFontAttributeName : font}];
- dev4u[<UITextField 0x11561d90> valueForUndefinedKey:]:该类不支持键值编码_compliant for the key _field.
- i_am_jorf