123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // KMHeaderFooterFontInfoView.swift
- // Cisdem PDFMaster
- //
- // Created by tangchao on 2022/12/27.
- //
- import Cocoa
- class KMHeaderFooterFontInfoView: KMHeaderFooterAdjectiveInfoBaseView {
- let fontSizes: Array<Int> = [8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72]
-
- var fontNameComboBox = NSComboBox()
- var fontSizeComboBox = NSComboBox()
- var colorBox = NSView()
- var colorView = NSView()
- var colorPlateButton = NSButton()
-
- override func initSubviews() {
- super.initSubviews()
-
- self.addSubview(self.fontNameComboBox)
- self.addSubview(self.fontSizeComboBox)
- self.addSubview(self.colorBox)
-
- self.colorBox.addSubview(self.colorView)
- self.colorBox.addSubview(self.colorPlateButton)
-
- self.titleLabel.stringValue = NSLocalizedString("Font", comment: "")
-
- let fonts = KMWatermarkAdjectiveTools.fetchAvailableFonts(10)
- self.fontNameComboBox.addItems(withObjectValues: fonts)
- self.fontNameComboBox.delegate = self
- self.fontNameComboBox.isEditable = false
-
- for i in self.fontSizes {
- self.fontSizeComboBox.addItem(withObjectValue: "\(i)")
- }
- self.fontSizeComboBox.delegate = self
- self.fontSizeComboBox.isEditable = false
-
- self.colorBox.wantsLayer = true
- self.colorBox.layer?.borderWidth = 1
- self.colorBox.layer?.borderColor = NSColor.black.cgColor
- self.colorView.wantsLayer = true
- self.colorView.layer?.cornerRadius = 10
-
- self.colorPlateButton.isBordered = false
- self.colorPlateButton.title = ""
- self.colorPlateButton.target = self
- self.colorPlateButton.action = #selector(colorPlateButtonAction)
- }
- override func layout() {
- super.layout()
-
- let leftMargin: CGFloat = self.contentInset.left
- let contentWidth = NSWidth(self.bounds)-2*leftMargin
- let fontSizeWidth: CGFloat = 80
-
- self.fontNameComboBox.frame = NSMakeRect(leftMargin, self.titleLabel.frame.maxY+12, contentWidth-fontSizeWidth-10, 22)
- self.fontSizeComboBox.frame = NSMakeRect(self.fontNameComboBox.frame.maxX+10, self.titleLabel.frame.maxY+12, fontSizeWidth, 22)
-
- self.colorBox.frame = NSMakeRect(leftMargin, self.fontNameComboBox.frame.maxY+12, 66, 32)
- let colorItemSize: CGFloat = 20
- self.colorView.frame = NSMakeRect(8, 6, colorItemSize, colorItemSize)
- self.colorPlateButton.frame = NSMakeRect(self.colorView.frame.maxX+10, 6, colorItemSize, colorItemSize)
- }
-
- override var model: KMHeaderFooterAdjectiveModel {
- get {
- return super.model
- }
- set {
- super.model = newValue
-
- switch newValue.textFont {
- case .font(name: let name, size: let size):
- let string = KMWatermarkAdjectiveTools.fontNameToAttribute(name, 10)
- if (string != nil) {
- self.fontNameComboBox.selectItem(withObjectValue: string)
- } else {
- self.fontNameComboBox.selectItem(at: 0)
- }
-
- self.fontSizeComboBox.stringValue = String(format: "%.0f", size)
- default:
- break
- }
-
- switch newValue.textColor {
- case .color(red: let red, green: let green, blue: let blue, alpha: let alpha):
- self.colorView.layer?.backgroundColor = NSColor(calibratedRed: red, green: green, blue: blue, alpha: alpha).cgColor
- default:
- break
- }
- }
- }
-
- @objc func colorPlateButtonAction() {
- guard let callback = itemClick else {
- return
- }
-
- callback(3, "")
- }
- }
- extension KMHeaderFooterFontInfoView: NSComboBoxDelegate {
- func comboBoxSelectionDidChange(_ notification: Notification) {
- if (self.fontNameComboBox.isEqual(to: notification.object)) {
- guard let callback = itemClick else {
- return
- }
- var index: Int = self.fontSizeComboBox.indexOfSelectedItem
- if (index < 0) {
- index = 0
- }
-
-
- callback(1, KMWatermarkAdjectiveTools.fetchAvailableFonts(10)[index].string)
- } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
- guard let callback = itemClick else {
- return
- }
-
- var index: Int = self.fontSizeComboBox.indexOfSelectedItem
- if (index < 0) {
- index = 0
- }
- callback(2, self.fontSizes[index])
- }
- }
- }
|