macos開發 - 10.14 下光標、placeholder 不見了

 

問題:

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!
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章