如何在iOS中使用Swift的属性占位符

Ste*_*Fox 3 ios swift

我在函数调用中的最后一个参数有困难..

        myTextField.attributedPlaceholder = NSAttributedString(string: "Wage Type", attributes: NSForegroundColorAttributeName: ??????)
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用此方法设置占位符的颜色.

Rya*_*yan 10

所以你使用的初始化器看起来像这样:

init(string aString: String!, attributes attributes: [NSObject : AnyObject]!)
Run Code Online (Sandbox Code Playgroud)

期望字典将属性键映射到值.查看此文档以查找预期的键/值对.因此,如果您想要做的只是更改文本颜色,您可以尝试这样做:

let attributesDictionary = [NSForegroundColorAttributeName: UIColor.greenColor()]
myTextField.attributedPlaceholder = NSAttributedString(string: "Wage Type", attributes: attributesDictionary)
Run Code Online (Sandbox Code Playgroud)

如果您对此有任何疑问,请与我们联系!