KMCustomTextField.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // KMCustomTextField.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. // 自定义输入框
  8. class KMCustomTextField: KMHoverView {
  9. private var _backgroundView = NSView()
  10. var backgroundView: NSView {
  11. get {
  12. return self._backgroundView
  13. }
  14. }
  15. private var _textFiled = KMTextField()
  16. var rightView: NSView? {
  17. willSet {
  18. self.rightView?.removeFromSuperview()
  19. }
  20. didSet {
  21. if let view = self.rightView {
  22. self.addSubview(view)
  23. self.layoutSubtreeIfNeeded()
  24. }
  25. self.updateRightViewStateIfNeed(editing: self.kmEnabled)
  26. }
  27. }
  28. var rightViewMode: KMSecureTextFiledViewMode = .whileEditing {
  29. didSet {
  30. self.updateRightViewStateIfNeed(editing: self.kmEnabled)
  31. }
  32. }
  33. var kmEnabled = true {
  34. didSet {
  35. self._textFiled.isEnabled = self.kmEnabled
  36. self.updateRightViewStateIfNeed(editing: self.kmEnabled)
  37. }
  38. }
  39. var placeholderString: String? {
  40. didSet {
  41. self._textFiled.placeholderString = self.placeholderString ?? ""
  42. }
  43. }
  44. var stringValue: String? {
  45. get {
  46. return self._textFiled.stringValue
  47. }
  48. set {
  49. self._textFiled.stringValue = newValue ?? ""
  50. }
  51. }
  52. var borderThickness: CGFloat = 1 {
  53. didSet {
  54. self._textFiled.borderThickness = self.borderThickness
  55. }
  56. }
  57. var offset: CGFloat = 8.0 {
  58. didSet {
  59. self._textFiled.offset = self.offset
  60. self.needsLayout = true
  61. }
  62. }
  63. weak var delegate: KMTextFieldDelegate?
  64. override init(frame frameRect: NSRect) {
  65. super.init(frame: frameRect)
  66. self.initSubViews()
  67. self.initDefaultValue()
  68. }
  69. required init?(coder: NSCoder) {
  70. super.init(coder: coder)
  71. }
  72. override func awakeFromNib() {
  73. super.awakeFromNib()
  74. self.initSubViews()
  75. self.initDefaultValue()
  76. }
  77. func initDefaultValue() {
  78. self._textFiled.isBezeled = false
  79. self._textFiled.focusRingType = .none
  80. self._textFiled.lineBreakMode = .byTruncatingTail
  81. self._textFiled.drawsBackground = false
  82. self._textFiled.delegate = self
  83. self._textFiled.firstResponderHandler = { [unowned self] result in
  84. self.updateRightViewStateIfNeed(editing: result)
  85. self.delegate?.km_didBecomeFirstResponder(textField: self)
  86. }
  87. }
  88. func initSubViews() {
  89. self.addSubview(self.backgroundView)
  90. self.addSubview(self._textFiled)
  91. }
  92. override func layout() {
  93. super.layout()
  94. let width: CGFloat = NSWidth(self.bounds)
  95. let height: CGFloat = NSHeight(self.bounds)
  96. self.backgroundView.frame = self.bounds
  97. var tfH: CGFloat = 22
  98. if let font = self._textFiled.font {
  99. tfH = font.pointSize * 1.5
  100. }
  101. // let tfX: CGFloat = 12
  102. let tfX: CGFloat = self.offset
  103. if let rightView = self.rightView {
  104. let rightWidth = NSWidth(rightView.frame)
  105. rightView.frame = NSMakeRect(width-rightWidth, 0, rightWidth, height)
  106. let textFieldFrame = NSMakeRect(tfX, (height-tfH)*0.5, width-tfX*2-rightWidth-5, tfH)
  107. self._textFiled.frame = textFieldFrame
  108. } else {
  109. let textFieldFrame = NSMakeRect(tfX, (height-tfH)*0.5, width-tfX*2, tfH)
  110. self._textFiled.frame = textFieldFrame
  111. }
  112. }
  113. func clear() {
  114. self._textFiled.stringValue = ""
  115. self.delegate?.km_controlTextDidChange(textField: self)
  116. }
  117. override func becomeFirstResponder() -> Bool {
  118. self.window?.makeFirstResponder(self._textFiled)
  119. return super.becomeFirstResponder()
  120. }
  121. internal func updateRightViewStateIfNeed(editing: Bool) {
  122. guard let view = self.rightView else {
  123. return
  124. }
  125. if (self.rightViewMode == .always) {
  126. view.isHidden = false
  127. return
  128. }
  129. if (self.rightViewMode == .never) {
  130. view.isHidden = true
  131. return
  132. }
  133. if (editing) { // 开始编辑
  134. view.isHidden = self.rightViewMode != .whileEditing
  135. } else { // 结束编辑
  136. view.isHidden = self.rightViewMode == .whileEditing
  137. }
  138. }
  139. }
  140. extension KMCustomTextField: NSTextFieldDelegate {
  141. func control(_ control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {
  142. if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) {
  143. return should
  144. }
  145. return true
  146. }
  147. func controlTextDidBeginEditing(_ obj: Notification) {
  148. if (!self._textFiled.isEqual(to: obj.object)) {
  149. return
  150. }
  151. self.delegate?.km_controlTextDidBeginEditing(textField: self)
  152. }
  153. func controlTextDidChange(_ obj: Notification) {
  154. if (!self._textFiled.isEqual(to: obj.object)) {
  155. return
  156. }
  157. self.delegate?.km_controlTextDidChange(textField: self)
  158. }
  159. func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
  160. if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) {
  161. return should
  162. }
  163. return true
  164. }
  165. func controlTextDidEndEditing(_ obj: Notification) {
  166. if (!self._textFiled.isEqual(to: obj.object)) {
  167. return
  168. }
  169. self.updateRightViewStateIfNeed(editing: false)
  170. self.delegate?.km_controlTextDidEndEditing(textField: self)
  171. }
  172. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  173. switch commandSelector {
  174. case #selector(NSResponder.insertNewline(_:)):
  175. if let inputView = control as? NSTextField {
  176. // //当当前TextField按下enter
  177. if inputView == self._textFiled {
  178. // guard let callBack = enterAction else { return false}
  179. // callBack()
  180. }
  181. }
  182. return true
  183. default:
  184. return false
  185. }
  186. }
  187. }