swift 4,设置文本字段本地化字符串占位符颜色



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

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

这工作正常。

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

注意:我已经创建了可本地化的字符串文件

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'

我做得对吗?

尝试这样的东西:

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

使用常规UIColor并仅对attributedPlaceholder进行设置。

最新更新