KMHeaderFooterFontInfoView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // KMHeaderFooterFontInfoView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. import Cocoa
  8. class KMHeaderFooterFontInfoView: KMHeaderFooterAdjectiveInfoBaseView {
  9. let fontSizes: Array<Int> = [8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72]
  10. var fontNameComboBox = NSComboBox()
  11. var fontSizeComboBox = NSComboBox()
  12. var colorBox = NSView()
  13. var colorView = NSView()
  14. var colorPlateButton = NSButton()
  15. override func initSubviews() {
  16. super.initSubviews()
  17. self.addSubview(self.fontNameComboBox)
  18. self.addSubview(self.fontSizeComboBox)
  19. self.addSubview(self.colorBox)
  20. self.colorBox.addSubview(self.colorView)
  21. self.colorBox.addSubview(self.colorPlateButton)
  22. self.titleLabel.stringValue = NSLocalizedString("Font", comment: "")
  23. let fonts = KMWatermarkAdjectiveTools.fetchAvailableFonts(10)
  24. self.fontNameComboBox.addItems(withObjectValues: fonts)
  25. self.fontNameComboBox.delegate = self
  26. self.fontNameComboBox.isEditable = false
  27. for i in self.fontSizes {
  28. self.fontSizeComboBox.addItem(withObjectValue: "\(i)")
  29. }
  30. self.fontSizeComboBox.delegate = self
  31. self.fontSizeComboBox.isEditable = false
  32. self.colorBox.wantsLayer = true
  33. self.colorBox.layer?.borderWidth = 1
  34. self.colorBox.layer?.borderColor = NSColor.black.cgColor
  35. self.colorView.wantsLayer = true
  36. self.colorView.layer?.cornerRadius = 10
  37. self.colorPlateButton.isBordered = false
  38. self.colorPlateButton.title = ""
  39. self.colorPlateButton.target = self
  40. self.colorPlateButton.action = #selector(colorPlateButtonAction)
  41. }
  42. override func layout() {
  43. super.layout()
  44. let leftMargin: CGFloat = self.contentInset.left
  45. let contentWidth = NSWidth(self.bounds)-2*leftMargin
  46. let fontSizeWidth: CGFloat = 80
  47. self.fontNameComboBox.frame = NSMakeRect(leftMargin, self.titleLabel.frame.maxY+12, contentWidth-fontSizeWidth-10, 22)
  48. self.fontSizeComboBox.frame = NSMakeRect(self.fontNameComboBox.frame.maxX+10, self.titleLabel.frame.maxY+12, fontSizeWidth, 22)
  49. self.colorBox.frame = NSMakeRect(leftMargin, self.fontNameComboBox.frame.maxY+12, 66, 32)
  50. let colorItemSize: CGFloat = 20
  51. self.colorView.frame = NSMakeRect(8, 6, colorItemSize, colorItemSize)
  52. self.colorPlateButton.frame = NSMakeRect(self.colorView.frame.maxX+10, 6, colorItemSize, colorItemSize)
  53. }
  54. override var model: KMHeaderFooterAdjectiveModel {
  55. get {
  56. return super.model
  57. }
  58. set {
  59. super.model = newValue
  60. switch newValue.textFont {
  61. case .font(name: let name, size: let size):
  62. let string = KMWatermarkAdjectiveTools.fontNameToAttribute(name, 10)
  63. if (string != nil) {
  64. self.fontNameComboBox.selectItem(withObjectValue: string)
  65. } else {
  66. self.fontNameComboBox.selectItem(at: 0)
  67. }
  68. self.fontSizeComboBox.stringValue = String(format: "%.0f", size)
  69. default:
  70. break
  71. }
  72. switch newValue.textColor {
  73. case .color(red: let red, green: let green, blue: let blue, alpha: let alpha):
  74. self.colorView.layer?.backgroundColor = NSColor(calibratedRed: red, green: green, blue: blue, alpha: alpha).cgColor
  75. default:
  76. break
  77. }
  78. }
  79. }
  80. @objc func colorPlateButtonAction() {
  81. guard let callback = itemClick else {
  82. return
  83. }
  84. callback(3, "")
  85. }
  86. }
  87. extension KMHeaderFooterFontInfoView: NSComboBoxDelegate {
  88. func comboBoxSelectionDidChange(_ notification: Notification) {
  89. if (self.fontNameComboBox.isEqual(to: notification.object)) {
  90. guard let callback = itemClick else {
  91. return
  92. }
  93. var index: Int = self.fontSizeComboBox.indexOfSelectedItem
  94. if (index < 0) {
  95. index = 0
  96. }
  97. callback(1, KMWatermarkAdjectiveTools.fetchAvailableFonts(10)[index].string)
  98. } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
  99. guard let callback = itemClick else {
  100. return
  101. }
  102. var index: Int = self.fontSizeComboBox.indexOfSelectedItem
  103. if (index < 0) {
  104. index = 0
  105. }
  106. callback(2, self.fontSizes[index])
  107. }
  108. }
  109. }