KMTextField.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KMTextField.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. import Cocoa
  8. class KMTextField: NSTextField {
  9. private class _KMTextFieldCell: NSTextFieldCell {
  10. // 边框粗细
  11. var borderThickness: CGFloat = 1
  12. // 左右两边缩进
  13. var offset: CGFloat = 8.0
  14. override func drawingRect(forBounds theRect: NSRect) -> NSRect {
  15. var newRect = super.drawingRect(forBounds: theRect)
  16. let textSize = self.cellSize(forBounds: theRect)
  17. let heightInset = newRect.size.height - textSize.height
  18. if (heightInset > 0) {
  19. newRect.size.height = textSize.height
  20. newRect.origin.y += heightInset * 0.5
  21. } else {
  22. newRect.size.height = textSize.height
  23. newRect.origin.y += heightInset
  24. }
  25. newRect.origin.x += self.offset
  26. newRect.size.width = theRect.width - self.offset * 2
  27. return newRect
  28. }
  29. override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
  30. // Area that covers the NSTextField itself. That is the total height minus our custom border size.
  31. let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - self.borderThickness)
  32. let path = NSBezierPath()
  33. path.lineWidth = self.borderThickness
  34. // Line width is at the center of the line.
  35. path.move(to: NSPoint(x: 0, y: cellFrame.height))
  36. path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height))
  37. path.line(to: NSPoint(x: cellFrame.width, y: 0))
  38. path.line(to: NSPoint(x: 0, y: 0))
  39. NSColor.clear.setStroke()
  40. path.stroke()
  41. // Pass in area minus the border thickness in the height
  42. drawInterior(withFrame: interiorFrame, in: controlView)
  43. }
  44. }
  45. var borderThickness: CGFloat = 1 {
  46. didSet {
  47. if let _cell = self.cell as? _KMTextFieldCell {
  48. _cell.borderThickness = self.borderThickness
  49. }
  50. self.needsDisplay = true
  51. }
  52. }
  53. var offset: CGFloat = 8.0 {
  54. didSet {
  55. if let _cell = self.cell as? _KMTextFieldCell {
  56. _cell.offset = self.offset
  57. }
  58. self.needsDisplay = true
  59. }
  60. }
  61. override init(frame frameRect: NSRect) {
  62. super.init(frame: frameRect)
  63. // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result {
  64. // self.cell = _KMTextFieldCell()
  65. // }
  66. }
  67. var firstResponderHandler: ((Bool)->Void)?
  68. required init?(coder: NSCoder) {
  69. super.init(coder: coder)
  70. // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result {
  71. // self.cell = _KMTextFieldCell()
  72. // }
  73. }
  74. override class var cellClass: AnyClass? {
  75. set {
  76. super.cellClass = newValue
  77. }
  78. get {
  79. return _KMTextFieldCell.self
  80. }
  81. }
  82. override func becomeFirstResponder() -> Bool {
  83. let result = super.becomeFirstResponder()
  84. guard let callback = self.firstResponderHandler else {
  85. return result
  86. }
  87. callback(result)
  88. return result
  89. }
  90. }