KMTextfieldVC.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // KMTextfieldVC.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/1/31.
  6. //
  7. import Cocoa
  8. protocol KMTextfieldVCDelegate: NSObjectProtocol {
  9. func km_controlTextDidEndEditing(_ obj: KMTextfieldVC)
  10. func km_controlTextDidChange(_ obj: KMTextfieldVC)
  11. }
  12. class KMTextfieldCell: NSTextFieldCell {
  13. var borderThickness: CGFloat = 1
  14. var offset: CGFloat = 8.0
  15. override func drawingRect(forBounds theRect: NSRect) -> NSRect {
  16. var newRect:NSRect = super.drawingRect(forBounds: theRect)
  17. let textSize:NSSize = self.cellSize(forBounds: theRect)
  18. let heightDelta:CGFloat = newRect.size.height - textSize.height
  19. if heightDelta > 0 {
  20. newRect.size.height = textSize.height
  21. newRect.origin.y += heightDelta * 0.5
  22. } else {
  23. newRect.size.height = textSize.height
  24. newRect.origin.y += heightDelta
  25. }
  26. newRect.origin.x += offset
  27. newRect.size.width = theRect.width - offset*2
  28. return newRect
  29. }
  30. override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
  31. // Area that covers the NSTextField itself. That is the total height minus our custom border size.
  32. let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - borderThickness)
  33. let path = NSBezierPath()
  34. path.lineWidth = borderThickness
  35. // Line width is at the center of the line.
  36. path.move(to: NSPoint(x: 0, y: cellFrame.height))
  37. path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height))
  38. path.line(to: NSPoint(x: cellFrame.width, y: 0))
  39. path.line(to: NSPoint(x: 0, y: 0))
  40. NSColor.clear.setStroke()
  41. path.stroke()
  42. // Pass in area minus the border thickness in the height
  43. drawInterior(withFrame: interiorFrame, in: controlView)
  44. }
  45. }
  46. class KMTextfieldVC: NSViewController {
  47. @IBOutlet weak var mainBox: NSBox!
  48. @IBOutlet weak var textField: NSTextField!
  49. @IBOutlet weak var mainBoxHeight: NSLayoutConstraint!
  50. @IBOutlet weak var mainBoxWidth: NSLayoutConstraint!
  51. var height: Float = 32.0 // 高度
  52. var width: Float = 400.0 // 宽度
  53. var textColor: NSColor = .black // 内容颜色
  54. var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  55. var background: NSColor = .clear// 背景颜色
  56. var borderWidth: Float = 0.0// 边框宽度
  57. var borderColor: NSColor = .clear// 边框颜色
  58. var cornerRadius: Float = 0.0// 边框圆角
  59. var stringValue: String = ""// 内容
  60. var placeholderString: String = ""// 内容预设值
  61. var editable: Bool = true //是否允许编辑
  62. var becomeFirstResponder: Bool = false // 是否为第一响应者
  63. open weak var delete: KMTextfieldVCDelegate?
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. // Do view setup here.
  67. updateUI()
  68. }
  69. // MARK: Private Methods
  70. func updateUI() -> Void {
  71. // textField.stringValue = stringValue
  72. textField.placeholderString = placeholderString
  73. textField.textColor = textColor
  74. textField.font = font
  75. textField.isEditable = editable
  76. // if becomeFirstResponder {
  77. // textField.becomeFirstResponder()
  78. // }
  79. mainBox.fillColor = background
  80. mainBox.borderWidth = CGFloat(borderWidth)
  81. mainBox.borderColor = borderColor
  82. mainBox.cornerRadius = CGFloat(cornerRadius)
  83. mainBoxHeight.constant = CGFloat(height)
  84. mainBoxWidth.constant = CGFloat(width)
  85. }
  86. }
  87. extension KMTextfieldVC: NSTextFieldDelegate {
  88. func controlTextDidEndEditing(_ obj: Notification) {
  89. let object = obj.object as! NSTextField
  90. stringValue = object.stringValue
  91. self.delete?.km_controlTextDidEndEditing(self)
  92. }
  93. func controlTextDidChange(_ obj: Notification) {
  94. let object = obj.object as! NSTextField
  95. stringValue = object.stringValue
  96. self.delete?.km_controlTextDidChange(self)
  97. }
  98. }