swift 4,设置textField localizedString占位符COLOR

时间:2018-04-09 20:06:48

标签: ios swift uitextfield

使用attributionPlaceholder设置占位符和占位符颜色,如下所示:

textField.placeholder = "SOME TEXT"
textField.attributedPlaceholder = NSAttributedString(string: string ,attributes: [NSAttributedStringKey.foregroundColor: UIColor.black.cgColor])

这很好。

但是当像这样使用NSLocalizedString作为占位符时:

注意:我已创建Localizable.string文件

textField.placeholder = NSLocalizedString("textKey", comment: "Some comment")
textField.attributedPlaceholder = NSAttributedString(string: string ,attributes: [NSAttributedStringKey.foregroundColor: UIColor.black.cgColor])

此代码总是给我一个错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType set]: unrecognized selector sent to instance 0x1c40a9300'
我正在做对吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

let string = NSLocalizedString("textKey", comment: "Some comment")
textField.attributedPlaceholder = NSAttributedString(string: string ,attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])

使用常规UIColor并仅设置为attributedPlaceholder