KMSecureTextField.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // KMSecureTextField.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/25.
  6. //
  7. import Cocoa
  8. class KMSecureTextField: NSTextField {
  9. private var actualText: String = ""
  10. private var _isVisible: Bool = false
  11. var isVisible: Bool {
  12. get {
  13. return _isVisible
  14. }
  15. set {
  16. _isVisible = newValue
  17. if isVisible {
  18. super.stringValue = actualText
  19. } else {
  20. super.stringValue = String(repeating: "*", count: actualText.count)
  21. }
  22. }
  23. }
  24. override var stringValue: String {
  25. get {
  26. return actualText
  27. }
  28. set {
  29. actualText = newValue
  30. if isVisible {
  31. super.stringValue = actualText
  32. } else {
  33. super.stringValue = String(repeating: "*", count: actualText.count)
  34. }
  35. }
  36. }
  37. // MARK: Notification
  38. override func textDidChange(_ notification: Notification) {
  39. if let textField = notification.object as? NSTextField {
  40. actualText = textField.stringValue
  41. if isVisible {
  42. super.stringValue = actualText
  43. } else {
  44. super.stringValue = String(repeating: "*", count: actualText.count)
  45. }
  46. }
  47. if let textView = notification.object as? NSTextView {
  48. actualText = textView.string
  49. // if isVisible {
  50. // super.stringValue = actualText
  51. // } else {
  52. // super.stringValue = String(repeating: "*", count: actualText.count)
  53. // }
  54. }
  55. }
  56. }