Swift之设置UItextField的占位文字颜色颜色
第一步
Snip20180328_35.png
第二步
Snip20180328_36.png
import UIKit class LYWTextField: UITextField { let LYWPlaceholderLabelKey = "_placeholderLabel.textColor" override func awakeFromNib() { super.awakeFromNib() var count:UInt32 = 0 let vars = class_copyIvarList(UITextField.self, &count) for i in 0..<count { let ivar = vars![Int(i)] print("\(String(describing: ivar_getName(ivar)))") } tintColor = textColor _ = resignFirstResponder() } override func becomeFirstResponder() -> Bool { self.setValue(textColor, forKeyPath: LYWPlaceholderLabelKey) return super.becomeFirstResponder() } override func resignFirstResponder() -> Bool { self.setValue(UIColor.gray, forKeyPath: LYWPlaceholderLabelKey) return super.resignFirstResponder() } }
使用
Snip20180328_37.png
效果 文本框成为第一响应者时候,就是光标聚集,这个时候,占位文字颜色变成了红色
Snip20180328_38.png
为了对比:可以看下面原始的模样
Snip20180328_39.png
效果
Snip20180328_40.png
Demo :https://gitee.com/lanyingwei/codes/9i4fvetq67x3j0zhpsdlc41