我有很多 UITextfield,我想添加一个不同颜色的占位符。我试图添加扩展名而不是每次都重复代码以使其干净。我写过这样的东西,但无法继续。
我的 attributedPlaceholder
代码是
emailTextField.attributedPlaceholder = NSAttributedString(string:"EMAIL ADDRESS",
attributes:[NSForegroundColorAttributeName: UIColor.redColor()])
我的扩展:
import Foundation
import UIKit
extension NSAttributedString {
class func attributedPlaceholderString(string: String, withColor color: UIColor) -> NSAttributedString {
let attributedString = NSAttributedString(string:string, attributes:[NSForegroundColorAttributeName: color])
return attributedString
}
}
但是当我写的时候
emailTextField.attributedPlaceholder.
我无法获取我在扩展中创建的类的名称。请问我的问题在哪里?
最佳答案
代码是
emailTextField.attributedPlaceholder = NSAttributedString.attributedPlaceholderString("EMAIL ADDRESS", withColor: UIColor.redColor())
你必须考虑到你已经创建了一个类方法(通过使用 class
命令),所以你必须在类 NSAttributedString 上调用该方法
关于ios - 扩展 NSAttributedString 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33543362/