KMDesignTextField.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // KMDesignTextField.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. // 输入框 组件
  8. class KMDesignTextField: KMDesignBaseView {
  9. private var textField: KMCustomTextField?
  10. private var _designSize: KMDesignSize = .medium
  11. var designSize: KMDesignSize {
  12. get {
  13. return self._designSize
  14. }
  15. }
  16. var stringValue: String? {
  17. get {
  18. return self.textField?.stringValue
  19. }
  20. set {
  21. self.textField?.stringValue = newValue ?? ""
  22. }
  23. }
  24. override var kmEnabled: Bool {
  25. get {
  26. return super.kmEnabled
  27. }
  28. set {
  29. super.kmEnabled = newValue
  30. self.textField?.kmEnabled = newValue
  31. if (newValue) {
  32. // self.window?.makeFirstResponder(self.textField)
  33. } else {
  34. self.window?.makeFirstResponder(nil)
  35. }
  36. }
  37. }
  38. weak var delegate: KMTextFieldDelegate?
  39. convenience init(designSize: KMDesignSize = .medium) {
  40. self.init()
  41. self._designSize = designSize
  42. }
  43. override func initSubViews() {
  44. super.initSubViews()
  45. let textFiled = KMCustomTextField()
  46. self.addSubview(textFiled)
  47. self.textField = textFiled
  48. self.textField?.delegate = self
  49. }
  50. override func initDefaultValue() {
  51. super.initDefaultValue()
  52. self.textField?.backgroundView.wantsLayer = true
  53. self.textField?.hoverAction = { [unowned self] sender, action in
  54. if (!self.canHover()) {
  55. return
  56. }
  57. if (action == .enter) {
  58. self.state = .Hov
  59. } else if (action == .exit) {
  60. self.state = .Norm
  61. }
  62. }
  63. let model = KMDesignToken.shared.tokenUsesModel(withToken: self.find_bgToken(state: .Norm))
  64. if let data = model.height as? String, let _height = Float(data) {
  65. self.height = CGFloat(_height)
  66. }
  67. if let data = model.horizontalPadding as? String, let _vSpace = Float(data) {
  68. self.horizontalPadding = CGFloat(_vSpace)
  69. self.textField?.offset = self.horizontalPadding
  70. }
  71. self.state = .Norm
  72. }
  73. override func layout() {
  74. super.layout()
  75. let width: CGFloat = NSWidth(self.bounds)
  76. let heigth: CGFloat = NSHeight(self.bounds)
  77. let y: CGFloat = (heigth - self.height) * 0.5
  78. self.textField?.frame = NSMakeRect(0, y, width, self.height)
  79. }
  80. override func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey {
  81. if (state == .Norm) {
  82. return self.designSize == .medium ? "input.m.bg.norm" : "input.s.bg.norm"
  83. } else if (state == .Hov) {
  84. return self.designSize == .medium ? "input.m.bg.hov" : "input.s.bg.hov"
  85. } else if (state == .Focus) {
  86. return self.designSize == .medium ? "input.m.bg.focus" : "input.s.bg.focus"
  87. } else if (state == .Disabled) {
  88. return self.designSize == .medium ? "input.m.bg.dis" : "input.s.bg.dis"
  89. } else if (state == .Error_def) {
  90. return self.designSize == .medium ? "input.m.bg.error-def" : "input.s.bg.error-def"
  91. } else if (state == .Error_focus) {
  92. return self.designSize == .medium ? "input.m.bg.error-focus" : "input.s.bg.error-focus"
  93. }
  94. return KMDesignTokenNone
  95. }
  96. override func updateUI(state: KMDesignTokenState) {
  97. super.updateUI(state: state)
  98. self.textField?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor
  99. self.textField?.backgroundView.layer?.borderWidth = self.borderWidth
  100. self.textField?.backgroundView.layer?.borderColor = self.borderColor.cgColor
  101. self.textField?.backgroundView.layer?.cornerRadius = self.borderRadius
  102. }
  103. }
  104. extension KMDesignTextField: KMTextFieldDelegate {
  105. func km_didBecomeFirstResponder(textField: AnyObject) {
  106. if (textField.isEqual(to: self.textField)) {
  107. self.state = .Focus
  108. }
  109. self.delegate?.km_didBecomeFirstResponder(textField: self)
  110. }
  111. func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool {
  112. if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) {
  113. return should
  114. }
  115. return true
  116. }
  117. func km_controlTextDidBeginEditing(textField: AnyObject) {
  118. self.delegate?.km_controlTextDidBeginEditing(textField: self)
  119. }
  120. func km_controlTextDidChange(textField: AnyObject) {
  121. self.delegate?.km_controlTextDidChange(textField: self)
  122. }
  123. func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool {
  124. if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) {
  125. return should
  126. }
  127. return true
  128. }
  129. func km_controlTextDidEndEditing(textField: AnyObject) {
  130. if (textField.isEqual(to: self.textField)) {
  131. self.state = .Norm
  132. }
  133. self.delegate?.km_controlTextDidEndEditing(textField: self)
  134. }
  135. }