// // KMEditPDFTextPropertyViewController.swift // PDF Reader Pro // // Created by lxy on 2022/12/27. // import Cocoa enum CPDFActiveAreasAlignType : Int { case Left case Vertical case Right case Top case Horizontally case Bottom case DisHorizontally case DisVertical } class KMEditPDFTextPropertyViewController: NSViewController { @IBOutlet weak var contentView: NSClipView! @IBOutlet weak var backgroundView: NSView! @IBOutlet weak var headerBox: NSBox! @IBOutlet weak var imageContentView: NSView! @IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint! @IBOutlet weak var propertyTitle: NSTextField! @IBOutlet weak var preImageView: NSImageView! @IBOutlet weak var textPresuppositionContentView: NSView! @IBOutlet weak var textPresuppositionBox: NSBox! @IBOutlet weak var resetTextPresuppositionBox: NSBox! @IBOutlet weak var textPresuppositionTopContstraint: NSLayoutConstraint! @IBOutlet weak var textPresuppositionHeightContstraint: NSLayoutConstraint! @IBOutlet weak var fontContentView: NSView! @IBOutlet weak var fontTitleLabel: NSTextField! @IBOutlet weak var fontImageView: NSButton! @IBOutlet weak var fontContentViewHeightConstraint: NSLayoutConstraint! @IBOutlet weak var fontContentViewTopConstraint: NSLayoutConstraint! @IBOutlet weak var fontNameBox: NSBox! @IBOutlet weak var fontStyleBox: NSBox! @IBOutlet weak var fontSizeBox: NSBox! @IBOutlet weak var leftAlignmentBox: NSBox! @IBOutlet weak var centerAlignmentBox: NSBox! @IBOutlet weak var rightAlignmentBox: NSBox! @IBOutlet weak var colorBox: NSBox! @IBOutlet weak var fontColorButton: NSButton! @IBOutlet weak var fontCustomColorButton: NSButton! @IBOutlet weak var alignmentTopConstraint: NSLayoutConstraint! @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView! var textPresuppositionVC: KMDesignSelect? var textPresuppositionResetVC: KMDesignPropertySelector? var textPresuppositionResetString: String = "" var leftAlignmentVC: KMDesignPropertySelector? var centerAlignmentVC: KMDesignPropertySelector? var rightAlignmentVC: KMDesignPropertySelector? var fontNameVC: KMDesignSelect? var fontSizeVC: KMDesignSelect? var fontStyleVC: KMDesignSelect? var borderWidthVC: KMDesignSelect? var textsAreas : [CPDFEditTextArea] = [] var listView : CPDFListView! var fonts: [NSDictionary] = [] var currentColor : NSColor = NSColor.black var alignment : NSTextAlignment = .left var fontName: String = "Helvetica" { didSet { if self.fontNameVC != nil { if self.fontNameVC!.items.contains(fontName) { self.fontNameVC?.stringValue = fontName } else { self.fontNameVC?.stringValue = "Helvetica" } } } } var fontStyle: String = "Regular" { didSet { self.fontStyleVC?.stringValue = fontStyle } } var fontSize: CGFloat = 12 { didSet { self.fontSizeVC?.stringValue = Int(ceil(fontSize)).description + "pt" } } var selectAreas: [Any] { get { return self.listView.editingAreas() ?? [] } } deinit { KMPrint("KMEditPDFTextPropertyViewController 已释放.") NSColorPanel.shared.setTarget(nil) NSColorPanel.shared.setAction(nil) NSColorPanel.shared.close() self.removeNotification() } func addNotification() { NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil) } func removeNotification() { NotificationCenter.default.removeObserver(self) DistributedNotificationCenter.default().removeObserver(self) } @objc func changeEffectiveAppearance() { print("changeEffectiveAppearance") let isDarkModel = KMAdvertisementConfig.isDarkModel() if isDarkModel { self.view.appearance = NSAppearance(named: .darkAqua) } else { self.view.appearance = NSAppearance(named: .aqua) } self.updateUI() } func updateUI() { let isDarkModel = KMAdvertisementConfig.isDarkModel() if isDarkModel { self.backgroundView.backgroundColor(NSColor.km_init(hex: "#252526")) } else { self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA")) } } override func viewDidLoad() { super.viewDidLoad() self.addNotification() self.setup() self.initData() // self.reloadData() self.updateLanguage() self.updatePreviewImage() self.updateUI() } func setup() { // self.backgroundView.backgroundColor(KMAppearance.Layout.bgDrakColor()) self.propertyTitle.font = NSFont.SFProTextSemiboldFont(14.0) self.propertyTitle.textColor = KMAppearance.Layout.h0Color() self.fontTitleLabel.font = NSFont.SFProTextSemiboldFont(12.0) self.fontTitleLabel.textColor = KMAppearance.Layout.h0Color() self.headerBox.borderWidth = 1 self.headerBox.borderColor = KMAppearance.Interactive.s0Color() self.headerBox.cornerRadius = 4 self.headerBox.fillColor = KMAppearance.Layout.l1Color() // self.fontColorButton.border(NSColor.clear, 0, 10) //alignment leftAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn) leftAlignmentBox.contentView = leftAlignmentVC?.view leftAlignmentBox.fillColor = NSColor.clear leftAlignmentVC?.target = self leftAlignmentVC?.action = #selector(leftAlignmentAction) leftAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignLeft")! leftAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")! leftAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() leftAlignmentVC?.background_sel = KMAppearance.Layout.l1Color() leftAlignmentVC?.updateUI() centerAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn) centerAlignmentBox.contentView = centerAlignmentVC?.view centerAlignmentBox.fillColor = NSColor.clear centerAlignmentVC?.target = self centerAlignmentVC?.action = #selector(centerAlignmentAction) centerAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignCenter")! centerAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")! centerAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() centerAlignmentVC?.background_sel = KMAppearance.Layout.l1Color() centerAlignmentVC?.updateUI() rightAlignmentVC = KMDesignPropertySelector.init(withType: .Icon_Btn) rightAlignmentBox.contentView = rightAlignmentVC?.view rightAlignmentBox.fillColor = NSColor.clear rightAlignmentVC?.target = self rightAlignmentVC?.action = #selector(rightAlignmentAction) rightAlignmentVC?.image = NSImage(named: "KMImageNameEditPDFAlignRight")! rightAlignmentVC?.image_sel = NSImage(named: "KMImageNameEditPDFAlignRightSelect")! rightAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() rightAlignmentVC?.background_sel = KMAppearance.Layout.l1Color() rightAlignmentVC?.updateUI() let fontNameArray = KMEditPDFTextManager.manager.fetchTextFontNames() fontNameVC = KMDesignSelect.init(withType: .PopButton) // fontNameVC?.isScrollPop = true fontNameBox.contentView = fontNameVC?.view fontNameBox.fillColor = NSColor.clear fontNameVC?.addItems(withObjectValues: fontNameArray) fontNameVC?.selectItem(at: 0) fontNameVC?.delete = self fontNameVC?.borderColor = KMAppearance.Interactive.s0Color() fontNameVC?.background = KMAppearance.Layout.l1Color() fontNameVC?.background_hov = KMAppearance.Layout.l1Color() fontNameVC?.background_focus = KMAppearance.Layout.l1Color() fontNameVC?.textColor = KMAppearance.Layout.h1Color() fontNameVC?.textColor_hov = KMAppearance.Layout.h1Color() fontNameVC?.textColor_focus = KMAppearance.Layout.h1Color() fontNameVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() fontNameVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() fontNameVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() fontNameVC?.updateUI() let styleArray = defaultFontStyles //KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: fontName) fontStyleVC = KMDesignSelect.init(withType: .PopButton) fontStyleBox.contentView = fontStyleVC?.view fontStyleBox.fillColor = NSColor.clear fontStyleVC?.addItems(withObjectValues: styleArray) fontStyleVC?.selectItem(at: 0) fontStyleVC?.delete = self fontStyleVC?.borderColor = KMAppearance.Interactive.s0Color() fontStyleVC?.background = KMAppearance.Layout.l1Color() fontStyleVC?.background_hov = KMAppearance.Layout.l1Color() fontStyleVC?.background_focus = KMAppearance.Layout.l1Color() fontStyleVC?.textColor = KMAppearance.Layout.h1Color() fontStyleVC?.textColor_hov = KMAppearance.Layout.h1Color() fontStyleVC?.textColor_focus = KMAppearance.Layout.h1Color() fontStyleVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() fontStyleVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() fontStyleVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() fontStyleVC?.updateUI() fontSizeVC = KMDesignSelect.init(withType: .PopButton) fontSizeBox.contentView = fontSizeVC?.view fontSizeBox.fillColor = NSColor.clear fontSizeVC?.removeAllItems() fontSizeVC?.addItems(withObjectValues: self.supportFontSize()) fontSizeVC?.selectItem(at: 0) fontSizeVC?.delete = self fontSizeVC?.borderColor = KMAppearance.Interactive.s0Color() fontSizeVC?.background = KMAppearance.Layout.l1Color() fontSizeVC?.background_hov = KMAppearance.Layout.l1Color() fontSizeVC?.background_focus = KMAppearance.Layout.l1Color() fontSizeVC?.textColor = KMAppearance.Layout.h1Color() fontSizeVC?.textColor_hov = KMAppearance.Layout.h1Color() fontSizeVC?.textColor_focus = KMAppearance.Layout.h1Color() fontSizeVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() fontSizeVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() fontSizeVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() fontSizeVC?.updateUI() //textPresupposition textPresuppositionVC = KMDesignSelect.init(withType: .PopButton) textPresuppositionBox.contentView = textPresuppositionVC?.view textPresuppositionBox.fillColor = NSColor.clear textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: 0) textPresuppositionVC?.delete = self textPresuppositionVC?.borderColor = KMAppearance.Interactive.s0Color() textPresuppositionVC?.background = KMAppearance.Layout.l1Color() textPresuppositionVC?.background_hov = KMAppearance.Layout.l1Color() textPresuppositionVC?.background_focus = KMAppearance.Layout.l1Color() textPresuppositionVC?.textColor = KMAppearance.Layout.h1Color() textPresuppositionVC?.textColor_hov = KMAppearance.Layout.h1Color() textPresuppositionVC?.textColor_focus = KMAppearance.Layout.h1Color() textPresuppositionVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() textPresuppositionVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() textPresuppositionVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() textPresuppositionVC?.updateUI() textPresuppositionResetVC = KMDesignPropertySelector.init(withType: .Icon_Btn) resetTextPresuppositionBox.contentView = textPresuppositionResetVC?.view resetTextPresuppositionBox.fillColor = NSColor.clear textPresuppositionResetVC?.target = self textPresuppositionResetVC?.action = #selector(resetTextPresuppositionButtonAction) textPresuppositionResetVC?.image = NSImage(named: "KMImagePropertPanelTextDefaultMore")! textPresuppositionResetVC?.background_hov = KMAppearance.Layout.l1Color() textPresuppositionResetVC?.updateUI() // self.colorBox.borderColor = NSColor.km_init(hex: "#DFE1E5") self.colorBox.borderColor = KMAppearance.Interactive.s0Color() self.alignmentView.didChange = { [unowned self] view, areasArray, boundsArray in self.changeAreasAlign(areas: areasArray, newBounds: boundsArray) } } func initData() { self.updateTextPresupposition(type: .commonly, needChangeListView: false) let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) self.currentColor = model.color DispatchQueue.main.async { NSColorPanel.shared.color = model.color } self.fontColorButton.image = self.swatchWithColor(color: self.currentColor, size: NSSize(width: 20, height: 20)) self.fonts = CPDFAnnotationModel.supportFonts() as! [NSDictionary] } func updateLanguage() { let areas = self.selectAreas if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字 self.propertyTitle.stringValue = NSLocalizedString("General Properties", comment: "") } else { self.propertyTitle.stringValue = NSLocalizedString("Text", comment: "") } self.fontTitleLabel.stringValue = NSLocalizedString("Font", comment: "") } func reloadData() { if self.selectAreas.count > 0 { textsAreas = [] fontNameVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.fetchTextFontNames()) let areas = self.selectAreas self.alignmentView.editingAreas = areas for i in 0 ... areas.count-1 { if areas[i] is CPDFEditTextArea { textsAreas.append(areas[i] as! CPDFEditTextArea) } } if textsAreas.count == 1 && textsAreas.count == areas.count { self.headerBox.isHidden = false self.fontContentView.isHidden = false self.textPresuppositionContentView.isHidden = false self.imageViewHeightConstraint.constant = 88 self.textPresuppositionTopContstraint.constant = 152 self.fontContentViewHeightConstraint.constant = 153 self.alignmentTopConstraint.constant = 16 self.fontContentViewTopConstraint.constant = 8 } else if textsAreas.count > 1 && textsAreas.count == areas.count { self.headerBox.isHidden = true self.fontContentView.isHidden = false self.textPresuppositionContentView.isHidden = false self.imageViewHeightConstraint.constant = 0 self.textPresuppositionTopContstraint.constant = 50 self.fontContentViewHeightConstraint.constant = 153 self.alignmentTopConstraint.constant = 16 self.fontContentViewTopConstraint.constant = 8 } else if textsAreas.count > 0 && textsAreas.count != areas.count { //多选图片跟文字 self.headerBox.isHidden = true self.fontContentView.isHidden = true self.textPresuppositionContentView.isHidden = true self.fontContentViewHeightConstraint.constant = 0 self.textPresuppositionTopContstraint.constant = 16 self.imageViewHeightConstraint.constant = 0 self.alignmentTopConstraint.constant = 0 self.fontContentViewTopConstraint.constant = 0 } self.refreshSelectAreaProperty() self.updateLanguage() } } func refreshSelectAreaProperty(needDefaultData: Bool = false) { if self.selectAreas.count == 1 { let areas = self.selectAreas.first if areas is CPDFEditTextArea { var sizeString = "\(abs(self.listView.editingSelectionFontSize(with: areas as? CPDFEditTextArea)))" var fontName: String = self.listView.editingSelectionFontName(with: areas as? CPDFEditTextArea) ?? "Helvetica" let alignment = self.listView.editingSelectionAlignment(with: areas as? CPDFEditTextArea) let color = self.listView.editingSelectionFontColor(with: areas as? CPDFEditTextArea) ?? NSColor.black fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? []) if self.listView.isBoldCurrentSelection(with: areas as? CPDFEditTextArea) { fontName += "-Bold" } if self.listView.isItalicCurrentSelection(with: areas as? CPDFEditTextArea) { fontName += "-Italic" } //获取默认数据 if needDefaultData { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) fontName = model.fontName sizeString = model.fontSize.description } self.updateTextTextPresuppositionState() self.updateTextPresupposition(fontName: fontName, size: CGFloat(Float(sizeString)!), needChangeListView: false) self.currentColor = color DispatchQueue.main.async { NSColorPanel.shared.color = color } self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20)) self.alignment = alignment self.selectAlignment(alignment: alignment) self.updatePreviewImage() } } } override func mouseDown(with event: NSEvent) { } //MARK: ToolAction private func swatchWithColor(color:NSColor,size:NSSize) -> NSImage { let image = NSImage(size: size) image.lockFocus() color.drawSwatch(in: NSRect(x: 0, y: 0, width: size.width, height: size.height)) image.unlockFocus() return image } private func changeAreasAlign(areas:[Any],newBounds:[String]) { var oldBounds : [String] = [] for i in 0 ... areas.count-1 { let area : CPDFEditArea = areas[i] as! CPDFEditArea oldBounds.append(NSStringFromRect(area.bounds)) self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i])) } self.listView.setNeedsDisplayForVisiblePages() } } //MARK: - Action extension KMEditPDFTextPropertyViewController { @IBAction func fontColorAction(_ sender: Any) { let color = self.listView.editingSelectionFontColor(with: self.selectAreas.first as? CPDFEditTextArea) let panel = NSColorPanel.shared panel.setTarget(self) panel.setAction(#selector(fontColorChangeAction)) panel.orderFront(nil) panel.showsAlpha = false if color != nil { panel.color = color ?? NSColor.black } } @objc func fontColorChangeAction() { let color = NSColorPanel.shared.color self.currentColor = color self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20)) if self.selectAreas.count > 0 { self.listView.setEditingSelectionFontColor(color, with: self.selectAreas.first as? CPDFEditTextArea) } else { KMEditPDFTextManager.manager.setCommonlyFontColor(color: self.currentColor) } self.updatePreviewImage() } @objc func leftAlignmentAction() { self.leftAlignmentVC?.state = .Sel self.rightAlignmentVC?.state = .Norm self.centerAlignmentVC?.state = .Norm self.updateAlignment(alignment: .left) } @objc func centerAlignmentAction() { self.leftAlignmentVC?.state = .Norm self.rightAlignmentVC?.state = .Norm self.centerAlignmentVC?.state = .Sel self.updateAlignment(alignment: .center) } @objc func rightAlignmentAction() { self.leftAlignmentVC?.state = .Norm self.rightAlignmentVC?.state = .Sel self.centerAlignmentVC?.state = .Norm self.updateAlignment(alignment: .right) } func selectAlignment(alignment: NSTextAlignment) { switch alignment { case .left: self.leftAlignmentAction() case .right: self.rightAlignmentAction() case .center: self.centerAlignmentAction() default: self.leftAlignmentAction() } } @objc func resetTextPresuppositionButtonAction(sender: NSButton) { var popViewDataArr: [String] = [] var dataArray = [NSLocalizedString("Redefine", comment: ""), NSLocalizedString("Reset", comment: "")] for string in dataArray { popViewDataArr.append(string) } //调整保存参数 let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0 let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index]) let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type) var disItems: [String] = dataArray if model.change || model.redefine { disItems.removeObject(NSLocalizedString("Reset", comment: "")) } if model.change { disItems.removeObject(NSLocalizedString("Redefine", comment: "")) } let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr) vc.background = KMAppearance.Layout.bgColor() vc.textColor = KMAppearance.Layout.h0Color() vc.enterFillColor = KMAppearance.Interactive.s0Color() vc.disItems = disItems let createFilePopover: NSPopover = NSPopover.init() createFilePopover.contentViewController = vc createFilePopover.animates = true createFilePopover.behavior = .semitransient createFilePopover.setValue(true, forKey: "shouldHideAnchor") createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: -10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY) // vc.customBoxWidthLayoutConstraint.constant = self.popWidth ?? sender.frame.width vc.downCallback = { [unowned self](downEntered: Bool, count: String) -> Void in if downEntered { if count == NSLocalizedString("Reset", comment: "") { self.resetTextPresuppositionData() } else if count == NSLocalizedString("Redefine", comment: "") { self.reDefineTextPresuppositionData() } createFilePopover.close() } } } func updateTextTextPresuppositionState() { let areas = self.selectAreas.first if areas is CPDFEditTextArea { var size: CGFloat = (abs(self.listView.editingSelectionFontSize(with: areas as? CPDFEditTextArea))) var fontName: String = self.listView.editingSelectionFontName(with: areas as? CPDFEditTextArea) ?? "Helvetica" fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? []) if self.listView.isBoldCurrentSelection(with: areas as? CPDFEditTextArea) { fontName += "-Bold" } if self.listView.isItalicCurrentSelection(with: areas as? CPDFEditTextArea) { fontName += "-Italic" } let models = KMEditPDFTextManager.manager.fetchAllUserDefaultData() var index = 0 for i in 0...models.count - 1 { let model = models[i] if model.fontName == fontName && size == model.fontSize { index = i } } //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: index) } } } //MARK: - Update extension KMEditPDFTextPropertyViewController { func updateAlignment(alignment: NSTextAlignment) { if self.alignment != alignment { if self.selectAreas.count > 0 { self.listView.setCurrentSelectionAlignment(alignment, with: self.selectAreas.first as? CPDFEditTextArea) } else { KMEditPDFTextManager.manager.setFontAlignment(alignment: alignment) } self.alignment = alignment self.updatePreviewImage() } } func updateTextPresupposition(type: KMEditPDFTextFontType, needChangeListView: Bool = true) { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type) let fontName: String = model.fontName let size: CGFloat = model.fontSize self.updateTextPresupposition(fontName: fontName, size: size, needChangeListView: needChangeListView) //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) } } extension KMEditPDFTextPropertyViewController: KMSelectPopButtonDelegate { func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) { if obj == textPresuppositionVC { let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[obj.indexOfSelectedItem]) self.updateTextPresupposition(type: type) } else if obj == fontNameVC { self.updateFontNameAndStyle(name: obj.stringValue, style: fontStyle) self.updatePreviewImage() } else if obj == fontSizeVC { let size = CGFloat(Float((obj.stringValue.replacingOccurrences(of: "pt", with: "")))!) self.updateFontSize(size: size) self.updatePreviewImage() } else if obj == fontStyleVC { self.updateFontNameAndStyle(name: fontName, style: obj.stringValue) self.updatePreviewImage() } } } //MARK: - TextPresupposition extension KMEditPDFTextPropertyViewController { //MARK: 基本属性调整 func updateTextPresupposition(fontName: String, size: CGFloat, needChangeListView: Bool = true) { let fontNameArray = fontName.components(separatedBy: "-") var name = "" var style = "" if fontNameArray.count > 0 { name = fontNameArray.first! } else { name = "Helvetica" } if fontNameArray.count == 4 { style = "\(fontNameArray[2]) \(fontNameArray[3])" } else if fontNameArray.count == 3 { style = "\(fontNameArray[1]) \(fontNameArray[2])" } else if fontNameArray.count == 2 { style = fontNameArray.last! } else { style = "Regular" } self.updateFontNameAndStyle(name: name, style: style, needChangeListView: needChangeListView, needSave: false) self.updateFontSize(size: size, needChangeListView: needChangeListView, needSave: false) self.updatePreviewImage() } func resetTextPresuppositionData() { let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0 let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index]) KMEditPDFTextManager.manager.resetTextPresupposition(type: type) self.updateTextPresupposition(type: type) } func reDefineTextPresuppositionData(redefine: Bool = true) { let fontStyle = self.fontStyle let fontName = self.fontName + "-" + fontStyle let fontSize = self.fontSize let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0 let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index]) KMEditPDFTextManager.manager.reDefineTextPresupposition(fontName: fontName, fontSize: fontSize, type: type) self.updateTextPresupposition(type: type) } } //MARK: - Private extension KMEditPDFTextPropertyViewController { func updateFontNameAndStyle(name: String, style: String, needChangeListView: Bool = true, needSave: Bool = true) { // if fontName != name || fontStyle != style { let styleArray = defaultFontStyles //KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: name) var styleString = KMEditPDFTextManager.manager.checkFontStyle(style: style) if !styleArray.contains(style) { self.fontStyleVC?.addItems(withObjectValues: styleArray) self.fontStyleVC?.selectItem(at: 0) styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue) } else { self.fontStyleVC?.selectItem(at: styleArray.firstIndex(of: styleString) ?? 0) } var fontName = "\(name)-\(styleString)" if styleString.count == 0 { fontName = name } //数据保存返回 if self.selectAreas.count > 0 { if needChangeListView { self.listView.setEditingSelectionFontName(name, with: self.selectAreas.first as? CPDFEditTextArea) if styleString.contains("Bold") { self.listView.setCurrentSelectionIsBold(true, with: self.selectAreas.first as? CPDFEditTextArea) } else { self.listView.setCurrentSelectionIsBold(false, with: self.selectAreas.first as? CPDFEditTextArea) } if styleString.contains("Oblique") || styleString.contains("Italic") { self.listView.setCurrentSelectionIsItalic(true, with: self.selectAreas.first as? CPDFEditTextArea) } else { self.listView.setCurrentSelectionIsItalic(false, with: self.selectAreas.first as? CPDFEditTextArea) } } } else { if needSave { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: fontName, fontSize: model.fontSize, type: .commonly) } } self.fontName = name self.fontStyle = self.fontStyleVC?.stringValue ?? "Regular" if needSave { //调整保存参数 let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0 let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index]) let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: fontName, fontSize: model.fontSize, type: type) } //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) // } } func updateFontSize(size: CGFloat, needChangeListView: Bool = true, needSave: Bool = true) { // var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 8.0 if fontSize != size { if self.selectAreas.count > 0 { if needChangeListView { self.listView.setEditingSelectionFontSize(size, with: self.selectAreas.first as? CPDFEditTextArea, isAutoSize: true) } } else { if needSave { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontSize: size, type: .commonly) } } self.fontSize = size if needSave { let index = self.textPresuppositionVC?.indexOfSelectedItem ?? 0 let type = KMEditPDFTextFontType.typeOfRawValue(value: KMEditPDFTextFontType.allValues()[index]) let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontSize: size, type: type) } //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) } } //MARK: 刷新预览图片 private func updatePreviewImage() { var alignment = self.alignment var fontSize = Float((self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: ""))!) ?? 1.0 var fontName = self.fontNameVC?.stringValue ?? "Sample" let fontStyle = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC?.stringValue ?? "") let fontCurrentColor = self.currentColor fontName = KMEditPDFTextManager.manager.checkFontName(fontName: (fontName + "-" + fontStyle)) fontSize = max(fontSize, 8) let editringareas = self.selectAreas let count = editringareas.count // let editingSelectionString = self.listView.editingSelectionString() let editingSelectionAlignment = self.listView.editingSelectionAlignment(with: editringareas.first as? CPDFEditTextArea) if count == 1 { let areas = editringareas.first if areas is CPDFEditTextArea { // string = editingSelectionString ?? NSLocalizedString("Sample", comment: "") alignment = editingSelectionAlignment } } let image = KMEditPDFTextManager.manager.fetchTextImage(alignment: .center, fontName: fontName, fontSize: CGFloat(fontSize), color: fontCurrentColor, imageSize: self.preImageView.frame.size) self.preImageView.image = image } func supportFontSize() -> [String] { return ["8pt", "9pt", "10pt", "11pt", "12pt", "14pt", "16pt", "18pt", "20pt", "22pt", "24pt", "26pt", "28pt", "36pt", "48pt", "72pt"] } }