扩展类代码
import Foundation
import UIKit
extension UITextField{
var placeholderColor:UIColor {
get{
let color = self.value(forKeyPath: "_placeholderLabel.textColor")
if(color == nil){
return UIColor.white;
}
return color as! UIColor;
}
set{
self.setValue(newValue, forKeyPath: "_placeholderLabel.textColor")
}
}
var placeholderFont:UIFont{
get{
let font = self.value(forKeyPath: "_placeholderLabel.font")
if(font == nil){
return UIFont.systemFont(ofSize: 14);
}
return font as! UIFont;
}
set{
self.setValue(newValue, forKeyPath: "_placeholderLabel.font")
}
}
}
调用示例
let tf = UITextField()
tf.placeholder = "一段暂位文字"
tf.placeholderFont = FONT(32)
tf.placeholderColor = UIColor.white
tf.textAlignment = .center
tf.textColor = .white