ios 更改 UITextField 占位符颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21074417/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:05:29  来源:igfitidea点击:

Change of UITextField placeholder color

iosuitextfield

提问by theWalker

How to dynamically change placeholder color of the UITextField? This is always the same system color.

如何动态更改 的占位符颜色UITextField?这始终是相同的系统颜色。

No option in xib editor.

xib 编辑器中没有选项。

回答by DogCoffee

From Docs

来自文档

@property(nonatomic, copy) 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. 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.

@property(nonatomic, copy) NSAttributedString *attributedPlaceholder

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

Objective-C

目标-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

回答by iAhmed

Easy and perfect solution.

简单而完美的解决方案。

_placeholderLabel.textColor

In swift

在迅速

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

Objective-C

目标-C

UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
   [[NSAttributedString alloc]
   initWithString:@"Full Name"
   attributes:@{NSForegroundColorAttributeName:color}];

P.S Copied 3 different answers from Stackoverflow.

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

回答by Pradhyuman sinh

Use below code

使用下面的代码

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

回答by Medin Piranej

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

回答by Tomino

I use this in SWIFT:

我在 SWIFT 中使用它:

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

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

回答by Mohammad Kamran Usmani

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;

回答by Hardik Mamtora

You can use the following code

您可以使用以下代码

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

回答by Ron

This solution works without any subclassing and without any private 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
        }
    }
}

回答by Kuldeep

Try This.

尝试这个。

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

回答by rodrigoalvesvieira

@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