KMCustomTextField.swift 6.6 KB

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