12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import Cocoa
- class KMSecureTextField: NSTextField {
-
- private var actualText: String = ""
-
- private var _isVisible: Bool = false
-
- var isVisible: Bool {
- get {
- return _isVisible
- }
- set {
- _isVisible = newValue
- if isVisible {
- super.stringValue = actualText
- } else {
- super.stringValue = String(repeating: "*", count: actualText.count)
- }
- }
- }
-
- override var stringValue: String {
- get {
- return actualText
- }
- set {
- actualText = newValue
- if isVisible {
- super.stringValue = actualText
- } else {
- super.stringValue = String(repeating: "*", count: actualText.count)
- }
- }
- }
-
-
-
- override func textDidChange(_ notification: Notification) {
- if let textField = notification.object as? NSTextField {
- actualText = textField.stringValue
- if isVisible {
- super.stringValue = actualText
- } else {
- super.stringValue = String(repeating: "*", count: actualText.count)
- }
- }
- if let textView = notification.object as? NSTextView {
- actualText = textView.string
- }
- }
- }
|