KMWatermarkAdjectiveSegementControl.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KMWatermarkAdjectiveSegementControl.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/16.
  6. //
  7. import Cocoa
  8. class KMWatermarkAdjectiveSegementItemControl: NSControl {
  9. lazy var button: NSButton = {
  10. let button = NSButton()
  11. button.isBordered = false
  12. button.font = NSFont.systemFont(ofSize: 14)
  13. button.target = self
  14. button.action = #selector(buttonClick)
  15. return button
  16. }()
  17. private lazy var bottomLine: NSView = {
  18. let line = NSView()
  19. line.wantsLayer = true
  20. line.layer?.backgroundColor = NSColor.black.cgColor
  21. return line
  22. }()
  23. private var mySelected: Bool!
  24. var selected: Bool {
  25. get {
  26. return mySelected
  27. }
  28. set {
  29. mySelected = newValue
  30. if (newValue) {
  31. self.bottomLine.isHidden = false
  32. self.button.attributedTitle = NSAttributedString(string: self.button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.black, NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14)])
  33. } else {
  34. self.bottomLine.isHidden = true
  35. self.button.attributedTitle = NSAttributedString(string: self.button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor(white: 102.0/255.0, alpha: 1.0), NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14)])
  36. }
  37. }
  38. }
  39. override init(frame frameRect: NSRect) {
  40. super.init(frame: frameRect)
  41. self.initSubViews()
  42. }
  43. required init?(coder: NSCoder) {
  44. super.init(coder: coder)
  45. self.initSubViews()
  46. }
  47. func initSubViews() {
  48. self.addSubview(self.button)
  49. self.addSubview(self.bottomLine)
  50. }
  51. override func layout() {
  52. super.layout()
  53. button.frame = self.bounds
  54. bottomLine.frame = NSMakeRect(0, 0, NSWidth(self.bounds), 1)
  55. }
  56. @objc func buttonClick() {
  57. if ((self.target?.responds(to: self.action))!) {
  58. self.target?.perform(self.action, with: self)
  59. }
  60. }
  61. }
  62. typealias KMWatermarkAdjectiveSegementControlItemClick = (_ index: Int) -> ()
  63. class KMWatermarkAdjectiveSegementControl: NSView {
  64. private var itemControls: Array<KMWatermarkAdjectiveSegementItemControl> = []
  65. private var myItems: Array<String>!
  66. var items: Array<String> {
  67. get {
  68. return myItems
  69. }
  70. set {
  71. myItems = newValue
  72. /// 清理数据
  73. for itemControl in self.itemControls {
  74. itemControl.removeFromSuperview()
  75. }
  76. self.itemControls.removeAll()
  77. /// 添加数据
  78. for titleValue in newValue {
  79. let itemControl = KMWatermarkAdjectiveSegementItemControl()
  80. itemControl.button.title = titleValue
  81. itemControl.target = self
  82. itemControl.action = #selector(itemControlDidSelected)
  83. self.addSubview(itemControl)
  84. self.itemControls.append(itemControl)
  85. }
  86. self.selectItemIndex = mySelectItemIndex
  87. self.layoutSubtreeIfNeeded()
  88. }
  89. }
  90. private var mySelectItemIndex: Int = 0
  91. var selectItemIndex: Int {
  92. get {
  93. return mySelectItemIndex
  94. }
  95. set {
  96. mySelectItemIndex = newValue
  97. var i: Int = 0
  98. for itemControl in itemControls {
  99. if (i == newValue) {
  100. itemControl.selected = true
  101. } else {
  102. itemControl.selected = false
  103. }
  104. i += 1
  105. }
  106. }
  107. }
  108. var itemClick: KMWatermarkAdjectiveSegementControlItemClick!
  109. override init(frame frameRect: NSRect) {
  110. super.init(frame: frameRect)
  111. }
  112. required init?(coder: NSCoder) {
  113. super.init(coder: coder)
  114. }
  115. override func layout() {
  116. super.layout()
  117. let width: CGFloat = NSWidth(self.bounds)
  118. let height: CGFloat = NSHeight(self.bounds)
  119. var i: Int = 0
  120. var itemW: CGFloat = width * 0.5
  121. if (self.itemControls.count > 0) {
  122. itemW = width / CGFloat(self.itemControls.count)
  123. }
  124. for itemControl in self.itemControls {
  125. itemControl.frame = NSMakeRect(CGFloat(i) * itemW, 0, itemW, height)
  126. i += 1
  127. }
  128. }
  129. @objc func itemControlDidSelected(sender: KMWatermarkAdjectiveSegementItemControl) {
  130. var index: Int = 0
  131. for itemControl in itemControls {
  132. if (itemControl.isEqual(to: sender)) {
  133. break
  134. }
  135. index += 1
  136. }
  137. self.selectItemIndex = index;
  138. guard let callback = itemClick else {
  139. return
  140. }
  141. callback(self.selectItemIndex)
  142. }
  143. }