swift 4、设置textField localizedString占位符COLOR
swift 4, Set textField localizedString placeholder COLOR
使用 attributedPlaceholder 设置占位符和占位符颜色,如下所示:
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'
我做的对吗?
试试这样的东西:
let string = NSLocalizedString("textKey", comment: "Some comment")
textField.attributedPlaceholder = NSAttributedString(string: string ,attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])
使用常规 UIColor
并仅设置为 attributedPlaceholder
。
使用 attributedPlaceholder 设置占位符和占位符颜色,如下所示:
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'
我做的对吗?
试试这样的东西:
let string = NSLocalizedString("textKey", comment: "Some comment")
textField.attributedPlaceholder = NSAttributedString(string: string ,attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])
使用常规 UIColor
并仅设置为 attributedPlaceholder
。