问题:
10.14
下光标、placeholder 不见了
其实不是光标不见了,只是颜色是白色。
placeholder 可以使用上述方法(设置 setPlaceholderAttributedString)来设置。
解决:
分别创建NSTextField 和 NSScureTextField 的子类, 在子类方法中, 重写becomeFirstResponse方法, 在当前的textField变为第一响应的时候 对其做一些改变.
在becomeFirstResponder方法内, 写入改变光标颜色的方法. 由于[self currentEditor] 总是会返回当前正在编辑的NSTextView, 所以可以使用NSTextView的insertPointColor方法来改变光标颜色. 具体代码如下
override func becomeFirstResponder() -> Bool {
let success = super.becomeFirstResponder()
if success {
let textview = self.currentEditor() as? NSTextView
textview?.insertionPointColor = NSColor.red
}
return success
}
有些文章中介绍说要自定义NSTextFieldCell和NSSecureTextField, 在其内部重写setUpFieldEditorAttributes:方法, 来对当前正在编辑的NSTextView进行设置setInsetPointColor,虽然原理是一样的, 但是实际操作中发现, 重写cell的setUpFieldEditorAttributes方法, 对NSSecureTextField并不起作用, 而且还会影响到SecureTextField的focusRing.
override func setUpFieldEditorAttributes(_ textObj: NSText) -> NSText {
let text = super.setUpFieldEditorAttributes(textObj) as? NSTextView
text?.insertionPointColor = color_main_50
return text!
}