gpt4 book ai didi

swift - 以编程方式设置 UITextField 占位符颜色

转载 作者:IT王子 更新时间:2023-10-29 05:40:29 41 4
gpt4 key购买 nike

如何在 swift 中以编程方式设置 UITextField 占位符颜色?

最佳答案

1 - 用你想要的颜色创建一个 AttributedString。
2 - 将此 AttributedString 设置为文本字段的 attributedPlaceholder 属性

let placeholder = NSAttributedString(string: "Some", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 30));
textField.attributedPlaceholder = placeholder;
self.view.addSubview(textField)

来自苹果文档

var attributedPlaceholder: NSAttributedString!
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.

关于swift - 以编程方式设置 UITextField 占位符颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679075/

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