我有一个实现深蓝色的设计UITextField,因为占位符文本默认为深灰色,我几乎无法确定占位符文本所说的内容.
我当然用Google搜索了问题,但我还没有提出解决方案,而使用Swift语言而不是Obj-c.
有没有办法在UITextField使用Swift中更改占位符文本颜色?
vac*_*ama 459
您可以使用属性字符串设置占位符文本.通过以下方式传递您想要的颜色attributes:
var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSForegroundColorAttributeName: UIColor.yellow])
Run Code Online (Sandbox Code Playgroud)
对于Swift 3+使用以下:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
Run Code Online (Sandbox Code Playgroud)
对于Swift 4.2使用以下:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
Run Code Online (Sandbox Code Playgroud)
cru*_*bio 267
您可以使用Interface Builder快速完成此操作,而无需添加任何代码.
选择UITextField并打开右侧的身份检查器:
单击加号按钮并添加新的运行时属性:
placeholderLabel.textColor(Swift 4)
_placeholderLabel.textColor(Swift 3或更低版本)
使用颜色作为类型并选择颜色.
而已!
在您再次运行应用程序之前,您不会看到结果.
jos*_*405 122
UITextField像这样创建扩展:
extension UITextField{
@IBInspectable var placeHolderColor: UIColor? {
get {
return self.placeHolderColor
}
set {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: newValue!])
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的故事板或.xib中.你会看见
Tej*_*der 24
此代码适用于Swift3:
yourTextFieldName .setValue(UIColor.init(colorLiteralRed: 80/255, green: 80/255, blue: 80/255, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
Run Code Online (Sandbox Code Playgroud)
如果您有任何问题,请告诉我.
Sre*_*M S 24
在Swift 3.0中,使用
let color = UIColor.lightText
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
Run Code Online (Sandbox Code Playgroud)
小智 14
要为UITextField应用中的所有内容设置一次占位符颜色,您可以执行以下操作:
UILabel.appearanceWhenContainedInInstancesOfClasses([UITextField.self]).textColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)
这将为TextField整个应用程序中的所有占位符设置所需的颜色.但它仅在iOS 9之后可用.
在iOS 9中没有appearenceWhenContainedIn ....()方法在swift中但是你可以使用这里提供的一个解决方案在Swift中使用appearanceWhenContainedIn
对于斯威夫特
创建 UITextField 扩展
extension UITextField{
func setPlaceHolderColor(){
self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : UIColor.white])
}
}
Run Code Online (Sandbox Code Playgroud)
如果您是从故事板设置的。
extension UITextField{
@IBInspectable var placeHolderColor: UIColor? {
get {
return self.placeHolderColor
}
set {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor : newValue!])
}
}
}
Run Code Online (Sandbox Code Playgroud)
Xcode 9.2 Swift 4
extension UITextField{
@IBInspectable var placeHolderColor: UIColor? {
get {
return self.placeHolderColor
}
set {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: newValue!])
}
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 4:
txtControl.attributedPlaceholder = NSAttributedString(string: "Placeholder String...",attributes: [NSAttributedStringKey.foregroundColor: UIColor.gray])
Run Code Online (Sandbox Code Playgroud)
目标-C:
UIColor *color = [UIColor grayColor];
txtControl.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder String..." attributes:@{NSForegroundColorAttributeName: color}];
Run Code Online (Sandbox Code Playgroud)
小智 5
就我而言,我使用Swift 4
我为UITextField创建扩展
extension UITextField {
func placeholderColor(color: UIColor) {
let attributeString = [
NSAttributedStringKey.foregroundColor: color.withAlphaComponent(0.6),
NSAttributedStringKey.font: self.font!
] as [NSAttributedStringKey : Any]
self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: attributeString)
}
}
Run Code Online (Sandbox Code Playgroud)
yourField.placeholderColor(color:UIColor.white)
| 归档时间: |
|
| 查看次数: |
149709 次 |
| 最近记录: |