gpt4 book ai didi

ios - 更改 UITextField 占位符颜色

转载 作者:IT王子 更新时间:2023-10-29 07:31:52 26 4
gpt4 key购买 nike

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

xib 编辑器中没有选项。

最佳答案

来自文档

@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.

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

let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str

关于ios - 更改 UITextField 占位符颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com