扩展 NSAttributedString 问题
Extension NSAttributedString Issue
我有很多 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.
我无法获取我在扩展程序中创建的 class 的名称。请问我的问题在哪里?
代码为
emailTextField.attributedPlaceholder = NSAttributedString.attributedPlaceholderString("EMAIL ADDRESS", withColor: UIColor.redColor())
您必须考虑到您已经创建了一个 class 方法(通过使用 class
命令),因此您必须在 class NSAttributedString
我有很多 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.
我无法获取我在扩展程序中创建的 class 的名称。请问我的问题在哪里?
代码为
emailTextField.attributedPlaceholder = NSAttributedString.attributedPlaceholderString("EMAIL ADDRESS", withColor: UIColor.redColor())
您必须考虑到您已经创建了一个 class 方法(通过使用 class
命令),因此您必须在 class NSAttributedString