123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- //
- // KMWatermarkTextView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/17.
- //
- import Cocoa
- class KMWatermarkTextView: KMWatermarkAdjectiveBaseView {
- @IBOutlet var textView: NSTextView!
- @IBOutlet weak var fontStyleComboBOx: NSComboBox!
- @IBOutlet weak var fontSizeComboBox: NSComboBox!
- @IBOutlet weak var aligementBox: NSBox!
- var aligementView: KMWatermarkAligementView!
- @IBOutlet weak var colorBox: NSBox!
- var colorView: KMWatermarkColorView!
-
- let fontSizes: Array<Int> = [8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72]
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- textView.wantsLayer = true
- textView.layer?.borderWidth = 1
- textView.layer?.borderColor = NSColor.black.cgColor
- textView.layer?.cornerRadius = 4
- textView.textContainerInset = NSSize(width: 8, height: 8)
- textView.font = NSFont.systemFont(ofSize: 14)
- textView.delegate = self
-
- let fonts = NSFontManager.shared.availableFontFamilies
- self.fontStyleComboBOx.isEditable = false
- self.fontStyleComboBOx.removeAllItems()
- for fontName in fonts {
- let font = NSFont(name: fontName, size: 10)
- let attribute = [NSAttributedString.Key.font : font]
- let string = NSAttributedString(string: fontName, attributes: attribute as [NSAttributedString.Key : Any])
- self.fontStyleComboBOx.addItem(withObjectValue: string)
- }
- self.fontStyleComboBOx.selectItem(at: 0)
- self.fontStyleComboBOx.delegate = self
- self.fontSizeComboBox.removeAllItems()
- for i in self.fontSizes {
- self.fontSizeComboBox.addItem(withObjectValue: "\(i)pt")
- }
- self.fontSizeComboBox.selectItem(at: 0)
- self.fontSizeComboBox.delegate = self
-
- let aligementView = KMWatermarkAligementView()
- aligementView.frame = self.aligementBox.contentView!.bounds
- aligementView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.aligementView = aligementView
- self.aligementBox.contentView?.addSubview(aligementView)
- aligementView.itemClick = {
- (index: Int) in
- guard let callback = self.itemClick else {
- return
- }
-
- callback(4, index)
- }
-
- let colorView = KMWatermarkColorView()
- colorView.frame = self.colorBox.contentView!.bounds
- colorView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.colorView = colorView
- self.colorBox.contentView?.addSubview(colorView)
- colorView.colorButton.wantsLayer = true
- colorView.colorButton.layer?.backgroundColor = NSColor.black.cgColor
- colorView.itemClick = {
- [self] (idx: Int) in
- if (idx == 2) {
- let panel = NSColorPanel.shared
- panel.setTarget(self)
- panel.setAction(#selector(colorPanelAction))
- panel.orderFront(nil)
- }
- }
- }
-
- @objc func colorPanelAction(sender: Any) {
- guard let callback = self.itemClick else {
- return
- }
- self.colorView.colorButton.layer?.backgroundColor = NSColorPanel.shared.color.cgColor
-
- callback(5, NSColorPanel.shared.color)
- }
-
- override func setModel(model: KMWatermarkModel) {
- super.setModel(model: model)
-
- self.textView.string = model.text
-
-
- let font = NSFont(name: model.getTextFontName(), size: 10)
- let attribute = [NSAttributedString.Key.font : font]
- let string = NSAttributedString(string: model.getTextFontName(), attributes: attribute as [NSAttributedString.Key : Any])
- self.fontStyleComboBOx.selectItem(withObjectValue: string)
- self.fontSizeComboBox.stringValue = "\(model.getTextFontSize())pt"
-
- if (model.textAligement == .left) {
- self.aligementView.selectIndex(index: 0)
- } else if (model.textAligement == .center) {
- self.aligementView.selectIndex(index: 1)
- } else if (model.textAligement == .right) {
- self.aligementView.selectIndex(index: 2)
- } else {
- self.aligementView.selectIndex(index: 0)
- }
-
- self.colorView.colorButton.layer?.backgroundColor = model.getTextColor().cgColor
- }
- }
- extension KMWatermarkTextView: NSTextViewDelegate {
- func textDidChange(_ notification: Notification) {
- if (self.textView.isEqual(to: notification.object)) {
- guard let callback = itemClick else {
- return
- }
-
- callback(1, self.textView.string)
- }
- }
- }
- extension KMWatermarkTextView: NSComboBoxDelegate {
- func comboBoxSelectionDidChange(_ notification: Notification) {
- if (self.fontStyleComboBOx.isEqual(to: notification.object)) {
- guard let callback = itemClick else {
- return
- }
-
- callback(2, self.fontStyleComboBOx.stringValue)
- } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
- guard let callback = itemClick else {
- return
- }
-
- var index = self.fontSizeComboBox.indexOfSelectedItem
- if (index < 0) {
- index = 0
- }
-
- callback(3, fontSizes[index])
- }
- }
- }
|