ios 使用 Swift 更改占位符文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26076054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Placeholder Text Color with Swift
提问by Alex Catchpole
I have a design that implements a dark blue UITextField
, as the placeholder text is by default a dark grey colour I can barely make out what the place holder text says.
我有一个实现深蓝色的设计UITextField
,因为占位符文本默认为深灰色,我几乎看不出占位符文本的内容。
I've googled the problem of course but I have yet to come up with a solution while using the Swift language and not Obj-c.
我当然已经用谷歌搜索了这个问题,但我还没有想出一个解决方案,同时使用 Swift 语言而不是 Obj-c。
Is there a way to change the placeholder text colour in a UITextField
using Swift?
有没有办法在UITextField
使用 Swift 时更改占位符文本颜色?
回答by vacawama
You can set the placeholder text using an attributed string. Pass the color you want with the attributes
:
您可以使用属性字符串设置占位符文本。传递你想要的颜色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])
For Swift 3+ use following:
对于 Swift 3+,请使用以下内容:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
For Swift 4.2 use following:
对于 Swift 4.2,请使用以下内容:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
回答by crubio
You can accomplish this quickly, without adding a line of code, using Interface Builder.
您可以使用 Interface Builder 快速完成此操作,而无需添加任何代码。
Select the UITextField
and open the identity inspector on the right:
选择UITextField
并打开右侧的身份检查器:
Click on the plus button and add a new runtime attribute:
单击加号按钮并添加新的运行时属性:
placeholderLabel.textColor(Swift 4)
placeholderLabel.textColor(Swift 4)
_placeholderLabel.textColor(Swift 3 or less)
_placeholderLabel.textColor(Swift 3 或更低版本)
Use Coloras type and select the color.
使用颜色作为类型并选择颜色。
That's it.
就是这样。
You wont see the result until you run your app again.
在您再次运行您的应用程序之前,您不会看到结果。
回答by jose920405
Create UITextField
Extension like this:
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!])
}
}
}
And in your storyboard or .xib. You will see
在你的故事板或 .xib 中。你会看见
回答by Sreedeepkesav M S
In Swift 3.0, Use
在 Swift 3.0 中,使用
let color = UIColor.lightText
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
In Siwft 5.0 + Use
在 Siwft 5.0 + 中使用
let color = UIColor.lightText
let placeholder = textField.placeholder ?? "" //There should be a placeholder set in storyboard or elsewhere string or pass empty
textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : color])
回答by Tejinder
This code is working in Swift3:
此代码适用于 Swift3:
yourTextFieldName .setValue(UIColor.init(colorLiteralRed: 80/255, green: 80/255, blue: 80/255, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
let me know if you have any issue.
如果您有任何问题,请告诉我。
回答by Krzynool
To set the placeholder color once for all the UITextField
in your app you can do:
要为UITextField
应用程序中的所有元素设置一次占位符颜色,您可以执行以下操作:
UILabel.appearanceWhenContainedInInstancesOfClasses([UITextField.self]).textColor = UIColor.redColor()
This will set the desired color for all TextField
placeholders in the entire app. But it is only available since iOS 9.
这将为TextField
整个应用程序中的所有占位符设置所需的颜色。但它仅从 iOS 9 开始可用。
There is no appearenceWhenContainedIn....() method before iOS 9 in swift but you can use one of the solutions provided here appearanceWhenContainedIn in Swift
在 swift 中的 iOS 9 之前没有 appearenceWhenContainedIn....() 方法,但您可以使用这里提供的解决方案之一 Swift 中的 appearanceWhenContainedIn
回答by Ridho Octanio
In my case, I use Swift 4
就我而言,我使用Swift 4
I create extension for UITextField
我为 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)
}
}
yourField.placeholderColor(color: UIColor.white)
yourField.placeholderColor(颜色:UIColor.white)
回答by Khawar Islam
Xcode 9.2 Swift 4
Xcode 9.2 斯威夫特 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!])
}
}
}
回答by ninja_iOS
Swift 3 (probably 2), you can override didSet on placeholder in UITextField
subclass to apply attribute on it, this way:
Swift 3(可能是 2),您可以覆盖UITextField
子类中占位符上的 didSet 以在其上应用属性,如下所示:
override var placeholder: String? {
didSet {
guard let tmpText = placeholder else {
self.attributedPlaceholder = NSAttributedString(string: "")
return
}
let textRange = NSMakeRange(0, tmpText.characters.count)
let attributedText = NSMutableAttributedString(string: tmpText)
attributedText.addAttribute(NSForegroundColorAttributeName , value:UIColor(white:147.0/255.0, alpha:1.0), range: textRange)
self.attributedPlaceholder = attributedText
}
}
回答by álvaro Agüero
Swift 4 :
斯威夫特 4:
txtControl.attributedPlaceholder = NSAttributedString(string: "Placeholder String...",attributes: [NSAttributedStringKey.foregroundColor: UIColor.gray])
Objective-C :
目标-C:
UIColor *color = [UIColor grayColor];
txtControl.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder String..." attributes:@{NSForegroundColorAttributeName: color}];