KMWatermarkTextView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // KMWatermarkTextView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/17.
  6. //
  7. import Cocoa
  8. class KMWatermarkTextView: KMWatermarkAdjectiveBaseView {
  9. @IBOutlet var textView: NSTextView!
  10. @IBOutlet weak var fontStyleComboBOx: NSComboBox!
  11. @IBOutlet weak var fontSizeComboBox: NSComboBox!
  12. @IBOutlet weak var aligementBox: NSBox!
  13. var aligementView: KMWatermarkAligementView!
  14. @IBOutlet weak var colorBox: NSBox!
  15. var colorView: KMWatermarkColorView!
  16. let fontSizes: Array<Int> = [8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72]
  17. override func awakeFromNib() {
  18. super.awakeFromNib()
  19. textView.wantsLayer = true
  20. textView.layer?.borderWidth = 1
  21. textView.layer?.borderColor = NSColor.black.cgColor
  22. textView.layer?.cornerRadius = 4
  23. textView.textContainerInset = NSSize(width: 8, height: 8)
  24. textView.font = NSFont.systemFont(ofSize: 14)
  25. textView.delegate = self
  26. let fonts = NSFontManager.shared.availableFontFamilies
  27. self.fontStyleComboBOx.isEditable = false
  28. self.fontStyleComboBOx.removeAllItems()
  29. for fontName in fonts {
  30. let font = NSFont(name: fontName, size: 10)
  31. let attribute = [NSAttributedString.Key.font : font]
  32. let string = NSAttributedString(string: fontName, attributes: attribute as [NSAttributedString.Key : Any])
  33. self.fontStyleComboBOx.addItem(withObjectValue: string)
  34. }
  35. self.fontStyleComboBOx.selectItem(at: 0)
  36. self.fontStyleComboBOx.delegate = self
  37. self.fontSizeComboBox.removeAllItems()
  38. for i in self.fontSizes {
  39. self.fontSizeComboBox.addItem(withObjectValue: "\(i)pt")
  40. }
  41. self.fontSizeComboBox.selectItem(at: 0)
  42. self.fontSizeComboBox.delegate = self
  43. let aligementView = KMWatermarkAligementView()
  44. aligementView.frame = self.aligementBox.contentView!.bounds
  45. aligementView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  46. self.aligementView = aligementView
  47. self.aligementBox.contentView?.addSubview(aligementView)
  48. aligementView.itemClick = {
  49. (index: Int) in
  50. guard let callback = self.itemClick else {
  51. return
  52. }
  53. callback(4, index)
  54. }
  55. let colorView = KMWatermarkColorView()
  56. colorView.frame = self.colorBox.contentView!.bounds
  57. colorView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  58. self.colorView = colorView
  59. self.colorBox.contentView?.addSubview(colorView)
  60. colorView.colorButton.wantsLayer = true
  61. colorView.colorButton.layer?.backgroundColor = NSColor.black.cgColor
  62. colorView.itemClick = {
  63. [self] (idx: Int) in
  64. if (idx == 2) {
  65. let panel = NSColorPanel.shared
  66. panel.setTarget(self)
  67. panel.setAction(#selector(colorPanelAction))
  68. panel.orderFront(nil)
  69. }
  70. }
  71. }
  72. @objc func colorPanelAction(sender: Any) {
  73. guard let callback = self.itemClick else {
  74. return
  75. }
  76. self.colorView.colorButton.layer?.backgroundColor = NSColorPanel.shared.color.cgColor
  77. callback(5, NSColorPanel.shared.color)
  78. }
  79. override func setModel(model: KMWatermarkModel) {
  80. super.setModel(model: model)
  81. self.textView.string = model.text
  82. let font = NSFont(name: model.getTextFontName(), size: 10)
  83. let attribute = [NSAttributedString.Key.font : font]
  84. let string = NSAttributedString(string: model.getTextFontName(), attributes: attribute as [NSAttributedString.Key : Any])
  85. self.fontStyleComboBOx.selectItem(withObjectValue: string)
  86. self.fontSizeComboBox.stringValue = "\(model.getTextFontSize())pt"
  87. if (model.textAligement == .left) {
  88. self.aligementView.selectIndex(index: 0)
  89. } else if (model.textAligement == .center) {
  90. self.aligementView.selectIndex(index: 1)
  91. } else if (model.textAligement == .right) {
  92. self.aligementView.selectIndex(index: 2)
  93. } else {
  94. self.aligementView.selectIndex(index: 0)
  95. }
  96. self.colorView.colorButton.layer?.backgroundColor = model.getTextColor().cgColor
  97. }
  98. }
  99. extension KMWatermarkTextView: NSTextViewDelegate {
  100. func textDidChange(_ notification: Notification) {
  101. if (self.textView.isEqual(to: notification.object)) {
  102. guard let callback = itemClick else {
  103. return
  104. }
  105. callback(1, self.textView.string)
  106. }
  107. }
  108. }
  109. extension KMWatermarkTextView: NSComboBoxDelegate {
  110. func comboBoxSelectionDidChange(_ notification: Notification) {
  111. if (self.fontStyleComboBOx.isEqual(to: notification.object)) {
  112. guard let callback = itemClick else {
  113. return
  114. }
  115. callback(2, self.fontStyleComboBOx.stringValue)
  116. } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
  117. guard let callback = itemClick else {
  118. return
  119. }
  120. var index = self.fontSizeComboBox.indexOfSelectedItem
  121. if (index < 0) {
  122. index = 0
  123. }
  124. callback(3, fontSizes[index])
  125. }
  126. }
  127. }