// // KMEditPDFTextPropertyViewController.swift // PDF Reader Pro // // Created by lxy on 2022/12/27. // import Cocoa class KMSliderItemView: NSView { var titleLabel: NSTextField { get { return self.titleLabel_ } } private lazy var titleLabel_: NSTextField = { let view = NSTextField(labelWithString: "") return view }() var slider: NSSlider { get { return self.slider_ } } private lazy var slider_: NSSlider = { let view = NSSlider() view.minValue = 0 view.maxValue = 1 view.target = self view.action = #selector(sliderValueDidChange) return view }() var box: NSBox { get { return self.box_ } } private lazy var box_: NSBox = { let view = NSBox() view.boxType = .custom view.titlePosition = .noTitle view.contentViewMargins = .zero view.borderWidth = 0 return view }() var minValue: Double = 0 { didSet { self.slider.minValue = self.minValue } } var maxValue: Double = 1 { didSet { self.slider.maxValue = self.maxValue } } var doubleValue: Double = 1 { didSet { self.slider.doubleValue = self.doubleValue } } var valueChange: ((Double)->Void)? convenience init() { self.init(frame: .zero) self.initSubViews() } func initSubViews() { self.addSubview(self.titleLabel_) self.addSubview(self.slider_) self.addSubview(self.box_) self.titleLabel_.km_add_top_constraint(constant: 0) self.titleLabel_.km_add_left_constraint(constant: 4) self.slider_.km_add_left_constraint(constant: 4) self.slider_.km_add_top_constraint(equalTo: self.titleLabel_, attribute: .bottom, constant: 4) self.slider_.km_add_height_constraint(constant: 26) self.slider_.km_add_right_constraint(equalTo: self.box_, attribute: .left, constant: -8) self.box_.km_add_right_constraint(constant: 0) self.box_.km_add_bottom_constraint(constant: -4) } @objc func sliderValueDidChange(_ sender: NSSlider) { self.valueChange?(sender.doubleValue) } } 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? @IBOutlet weak var fontBoldBox: NSBox! @IBOutlet weak var fontItalicBox: NSBox! var fontBoldVC: KMDesignButton? var fontItalicVC: KMDesignButton? @IBOutlet weak var fontAddBox: NSBox! @IBOutlet weak var fontReduceBox: NSBox! var fontAddVC: KMDesignButton? var fontReduceVC: KMDesignButton? @IBOutlet weak var opacityBox: NSBox! var opacitVC: 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) { let areas = self.handdler?.editingTextAreas ?? [] if areas.count == 1 { let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas.first) self.fontNameVC?.stringValue = cfont == nil ? "--" : fontName } else { self.fontNameVC?.stringValue = fontName } } else { // self.fontNameVC?.stringValue = "Helvetica" let areas = self.handdler?.editingTextAreas ?? [] if areas.count == 1 { let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas.first) self.fontNameVC?.stringValue = cfont == nil ? "--" : fontName } else { self.fontNameVC?.stringValue = fontName } } } } } var fontStyle: String = "Regular" { didSet { self.fontStyleVC?.stringValue = fontStyle } } var fontSize: CGFloat = 12 { didSet { if self.fontSize == -1 { self.fontSizeVC?.stringValue = "--" self.fontAddVC?.state = .Disabled self.fontReduceVC?.state = .Disabled } else { self.fontSizeVC?.stringValue = Int(ceil(fontSize)).description + "pt" self.fontAddVC?.state = .Norm self.fontReduceVC?.state = .Norm } } } var selectAreas: [Any] { get { return self.listView.editingAreas() ?? [] } } weak var handdler: KMEditPDfHanddler? deinit { KMPrint("KMEditPDFTextPropertyViewController 已释放.") NSColorPanel.shared.setTarget(nil) NSColorPanel.shared.setAction(nil) NSColorPanel.shared.close() self.removeNotification() } override func viewDidLoad() { super.viewDidLoad() self.addNotification() self.setup() self.initData() // self.reloadData() self.updateLanguage() self.updatePreviewImage() self.updateUI() self.headerBox.isHidden = true self.imageViewHeightConstraint.constant = 0 self.textPresuppositionTopContstraint.constant = 50 self.alignmentTopConstraint.constant = 16+50 } 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) self.fontColorButton.target = self self.fontColorButton.action = #selector(fontColorAction) //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.view_bg_dis_color() 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.view_bg_dis_color() 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.view_bg_dis_color() rightAlignmentVC?.updateUI() let fontNameArray = KMEditPDFTextManager.manager.fetchTextFontNames() let familyNames = CPDFFont.familyNames fontNameVC = KMDesignSelect.init(withType: .PopButton) // fontNameVC?.isScrollPop = true fontNameBox.contentView = fontNameVC?.view fontNameBox.fillColor = NSColor.clear fontNameVC?.addItems(withObjectValues: familyNames) 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() self.fontNameVC?.isScrollPop = true self.fontNameVC?.showVerticalScroller = true let styleArray = defaultFontStyles //KMEditPDFTextManager.manager.fetchFontStyleWithFontName(fontName: fontName) let fontStyleNames = CPDFFont.fontNames(forFamilyName: "Helvetica") fontStyleVC = KMDesignSelect.init(withType: .PopButton) fontStyleBox.contentView = fontStyleVC?.view fontStyleBox.fillColor = NSColor.clear fontStyleVC?.addItems(withObjectValues: fontStyleNames) 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() self.fontBoldVC = KMDesignButton(withType: .Image) self.fontBoldBox.contentView = self.fontBoldVC?.view self.fontBoldBox.borderWidth = 0 self.fontBoldVC?.target = self self.fontBoldVC?.action = #selector(fontBoldAction) self.fontBoldVC?.image = NSImage(named: "KMImageNameEditPDFFontBold")! self.fontBoldVC?.pagination() self.fontBoldVC?.initDefaultValue() self.fontItalicVC = KMDesignButton(withType: .Image) self.fontItalicBox.contentView = self.fontItalicVC?.view self.fontItalicBox.borderWidth = 0 self.fontItalicVC?.target = self self.fontItalicVC?.action = #selector(fontItalicAction) self.fontItalicVC?.image = NSImage(named: "KMImageNameEditPDFFontItalic")! self.fontItalicVC?.pagination() self.fontItalicVC?.initDefaultValue() fontSizeVC = KMDesignSelect.init(withType: .Combox) 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() self.fontSizeVC?.editable = true self.fontAddVC = KMDesignButton(withType: .Image) self.fontAddBox.contentView = self.fontAddVC?.view self.fontAddBox.borderWidth = 0 self.fontAddVC?.target = self self.fontAddVC?.action = #selector(fontAddAction) self.fontAddVC?.image = NSImage(named: "KMImageNameEditPDFFontAdd")! self.fontAddVC?.pagination() self.fontAddVC?.initDefaultValue() self.fontReduceVC = KMDesignButton(withType: .Image) self.fontReduceBox.contentView = self.fontReduceVC?.view self.fontReduceBox.borderWidth = 0 self.fontReduceVC?.target = self self.fontReduceVC?.action = #selector(fontReduceAction) self.fontReduceVC?.image = NSImage(named: "KMImageNameEditPDFFontReduce")! self.fontReduceVC?.pagination() self.fontReduceVC?.initDefaultValue() //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?.borderWidth = 1 textPresuppositionResetVC?.borderWidth_hov = 1 textPresuppositionResetVC?.borderWidth_sel = 1 textPresuppositionResetVC?.target = self textPresuppositionResetVC?.action = #selector(resetTextPresuppositionButtonAction) textPresuppositionResetVC?.image = NSImage(named: "KMImagePropertPanelTextDefaultMore")! textPresuppositionResetVC?._image_disabled = NSImage(named: "KMImagePropertPanelTextDefaultMore")! textPresuppositionResetVC?.background_hov = KMAppearance.Layout.l1Color() textPresuppositionResetVC?.background_disabled = KMAppearance.view_bg_dis_color() textPresuppositionResetVC?.cornerRadius = 4 textPresuppositionResetVC?.cornerRadius_hov = 4 textPresuppositionResetVC?.cornerRadius_disabled = 4 textPresuppositionResetVC?.borderWidth_disabled = 1 textPresuppositionResetVC?.borderColor_disabled = KMAppearance.view_border_dis_color() textPresuppositionResetVC?.updateUI() self.colorBox.borderColor = KMAppearance.Interactive.s0Color() let sliderItemView = KMSliderItemView() self.opacityBox.contentView = sliderItemView self.opacityBox.borderWidth = 0 sliderItemView.titleLabel.stringValue = NSLocalizedString("Opacity", comment: "") sliderItemView.minValue = 0.0001 sliderItemView.maxValue = 1 sliderItemView.valueChange = { [weak self] value in self?.opacitVC?.stringValue = String(format: "%0.f%%", value*100) if let data = self?.opacitVC { self?.km_comboBoxSelectionDidChange(data) } } opacitVC = KMDesignSelect.init(withType: .PopButton) sliderItemView.box.contentView = opacitVC?.view opacitVC?.addItems(withObjectValues: ["25%", "50%", "75%", "100%"]) opacitVC?.selectItem(at: 0) opacitVC?.delete = self opacitVC?.borderColor = KMAppearance.Interactive.s0Color() opacitVC?.background = KMAppearance.Layout.l1Color() opacitVC?.background_hov = KMAppearance.Layout.l1Color() opacitVC?.background_focus = KMAppearance.Layout.l1Color() opacitVC?.textColor = KMAppearance.Layout.h1Color() opacitVC?.textColor_hov = KMAppearance.Layout.h1Color() opacitVC?.textColor_focus = KMAppearance.Layout.h1Color() opacitVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() opacitVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() opacitVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() opacitVC?.updateUI() self.alignmentView.didChange = { [unowned self] view, areasArray, boundsArray in self.changeAreasAlign(areas: areasArray, newBounds: boundsArray) self._trackEvent() let textAreas = self.handdler?.editingTextAreas ?? [] let imageAreas = self.handdler?.editingImageAreas ?? [] if textAreas.count > 0 && imageAreas.count > 0 { self._trackAlignEvent() } } self.changeEffectiveAppearance() } 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 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() { 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")) self.textPresuppositionResetVC?.background_disabled = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.textPresuppositionResetVC?.borderColor = KMAppearance.Layout.w15Color() self.textPresuppositionResetVC?.borderColor_hov = KMAppearance.Interactive.a0Color() self.textPresuppositionResetVC?.borderColor_sel = KMAppearance.Layout.w15Color() self.textPresuppositionResetVC?.background = KMAppearance.Layout.l1Color() self.textPresuppositionResetVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.fontBoldVC?.background_act = KMAppearance.Interactive.m1Color() self.fontBoldVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.fontBoldVC?.borderColor_hov = KMAppearance.Interactive.a0Color() self.fontBoldVC?.borderWidth_act = 0 self.fontItalicVC?.background_act = KMAppearance.Interactive.m1Color() self.fontItalicVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.fontItalicVC?.borderColor_hov = KMAppearance.Interactive.a0Color() self.fontItalicVC?.borderWidth_act = 0 self.fontAddVC?.background_act = KMAppearance.Interactive.m1Color() self.fontAddVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.fontAddVC?.borderColor_hov = KMAppearance.Interactive.a0Color() self.fontReduceVC?.background_act = KMAppearance.Interactive.m1Color() self.fontReduceVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1) self.fontReduceVC?.borderColor_hov = KMAppearance.Interactive.a0Color() self.leftAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color() self.centerAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color() self.rightAlignmentVC?.background_sel = KMAppearance.Interactive.m1Color() } else { self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA")) self.propertyTitle.textColor = KMAppearance.Layout.h0Color() self.fontTitleLabel.textColor = KMAppearance.Layout.h0Color() self.headerBox.borderColor = KMAppearance.Interactive.s0Color() self.headerBox.fillColor = KMAppearance.Layout.l1Color() leftAlignmentBox.fillColor = NSColor.clear leftAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() leftAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color() centerAlignmentBox.fillColor = NSColor.clear centerAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() centerAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color() rightAlignmentBox.fillColor = NSColor.clear rightAlignmentVC?.background_hov = KMAppearance.Layout.l1Color() rightAlignmentVC?.background_sel = KMAppearance.view_bg_dis_color() 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() 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() 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() textPresuppositionBox.fillColor = NSColor.clear 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() textPresuppositionResetVC?.borderColor = NSColor(red: 223/255, green: 225/255, blue: 229/255, alpha: 1) textPresuppositionResetVC?.borderColor_hov = NSColor.clear textPresuppositionResetVC?.borderColor_sel = KMAppearance.Layout.w15Color() textPresuppositionResetVC?.borderColor_act = KMAppearance.Layout.w15Color() textPresuppositionResetVC?.borderColor_disabled = KMAppearance.Layout.w15Color() textPresuppositionResetVC?.background = NSColor.white textPresuppositionResetVC?.background_hov = KMAppearance.view_bg_dis_color() textPresuppositionResetVC?.background_disabled = KMAppearance.view_bg_dis_color() textPresuppositionResetVC?.borderColor_disabled = KMAppearance.view_border_dis_color() textPresuppositionResetVC?.borderWidth = 1 opacitVC?.borderColor = KMAppearance.Interactive.s0Color() opacitVC?.background = KMAppearance.Layout.l1Color() opacitVC?.background_hov = KMAppearance.Layout.l1Color() opacitVC?.background_focus = KMAppearance.Layout.l1Color() opacitVC?.textColor = KMAppearance.Layout.h1Color() opacitVC?.textColor_hov = KMAppearance.Layout.h1Color() opacitVC?.textColor_focus = KMAppearance.Layout.h1Color() opacitVC?.popViewControllerBackground = KMAppearance.Layout.bgColor() opacitVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color() opacitVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color() self.fontBoldVC?.initDefaultValue() self.fontItalicVC?.initDefaultValue() } } func reloadData() { if self.selectAreas.count > 0 { textsAreas = [] 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 = true self.fontContentView.isHidden = false self.textPresuppositionContentView.isHidden = false // 88 self.imageViewHeightConstraint.constant = 0 // 152 self.textPresuppositionTopContstraint.constant = 50 self.fontContentViewHeightConstraint.constant = 193 self.alignmentTopConstraint.constant = 16+50 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 = 193 self.alignmentTopConstraint.constant = 16+50 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 { let area = areas as! CPDFEditTextArea let cFont = self.listView.editingSelectionCFont(byRangeEdit: area) // let cFont = self.listView.editingSelectionCFont(with: area) var sizeString = "\(self.listView.editingSelectionFontSize(byRangeEdit: area))" var fontName: String = cFont?.familyName ?? "Helvetica" if fontName.count == 0 { fontName = "Helvetica" } var fontStyle: String = cFont?.styleName ?? "Regular" if fontStyle.count == 0 { fontStyle = "Regular" } let alignment = self.listView.currentSelectionAlignment(byRangeEdit: area) let color = self.listView.editingSelectionFontColor(byRangeEdit: area) ?? NSColor.black // fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? []) self.fontNameVC?.stringValue = cFont?.familyName == nil ? "--" : fontName // if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: area), data { // fontName += "-Bold" // } // if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: area), data { // fontName += "-Italic" // } //获取默认数据 if needDefaultData { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) fontName = model.fontName fontStyle = model.fontStyle sizeString = model.fontSize.description } self.updateTextTextPresuppositionState() // var fontN = fontName // if let data = cFont { // fontN = CPDFFont.convertAppleFont(data) ?? fontName // } self.updateTextPresupposition(fontName: fontName, style: fontStyle, 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() let opacity = self.listView.opacity(for: areas as? CPDFEditArea) let sliderView = self.opacityBox.contentView as? KMSliderItemView sliderView?.doubleValue = Double(opacity) self.opacitVC?.stringValue = String(format: "%0.f%%", opacity*100) if let value = self.listView?.isBoldCurrentSelection(byRangeEdit: area) { self.fontBoldVC?.state = value ? .Act : .Norm } if let value = self.listView?.isItalicCurrentSelection(byRangeEdit: area) { self.fontItalicVC?.state = value ? .Act : .Norm } } } else if self.selectAreas.count > 1 { if let data = self.handdler?.editAreasFontNameIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let font = self.listView.editingSelectionCFont(byRangeEdit: area) // let font = self.listView.editingSelectionCFont(with: area) self.fontNameVC?.stringValue = font?.familyName ?? "" } } else { self.fontNameVC?.stringValue = "--" } if let data = self.handdler?.editAreasFontStyleIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { // if let styleName = self.listView.editingSelectionCFont(with: area)?.styleName, styleName.isEmpty == false { if let styleName = self.listView.editingSelectionCFont(byRangeEdit: area)?.styleName, styleName.isEmpty == false { self.fontStyleVC?.stringValue = styleName } else { let styleNames = CPDFFont.fontNames(forFamilyName: self.listView.editingSelectionCFont(with: area)?.familyName ?? "") if let styleN = styleNames.first, styleN.isEmpty == false { self.fontStyleVC?.stringValue = styleN } else { self.fontStyleVC?.stringValue = "Regular" } } } } else { self.fontStyleVC?.stringValue = "--" } if let data = self.handdler?.editAreasFontBoldIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let value = self.listView.isBoldCurrentSelection(byRangeEdit: area) self.fontBoldVC?.state = value ? .Act : .Norm } else { self.fontBoldVC?.state = .Disabled } } else { self.fontBoldVC?.state = .Disabled } if let data = self.handdler?.editAreasFontItalicIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let value = self.listView.isItalicCurrentSelection(byRangeEdit: area) self.fontItalicVC?.state = value ? .Act : .Norm } else { self.fontItalicVC?.state = .Disabled } } else { self.fontItalicVC?.state = .Disabled } if let data = self.handdler?.editAreasFontSizeIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let data = self.listView.editingSelectionFontSize(with: area) self.fontSizeVC?.stringValue = String(format: "%.0fpt", data) } self.fontAddVC?.state = .Norm self.fontReduceVC?.state = .Norm self.fontAddVC?.button.isEnabled = true self.fontReduceVC?.button.isEnabled = true } else { self.fontSizeVC?.stringValue = "--" self.fontAddVC?.state = .Disabled self.fontReduceVC?.state = .Disabled self.fontAddVC?.button.isEnabled = false self.fontReduceVC?.button.isEnabled = false } if let data = self.handdler?.editAreasTextAlignmentIsEqual(), data { } else { self.leftAlignmentVC?.state = .Disabled self.centerAlignmentVC?.state = .Disabled self.rightAlignmentVC?.state = .Disabled } if let data = self.handdler?.editAreasFontColorIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let color = self.listView?.editingSelectionFontColor(byRangeEdit: area) ?? NSColor.black self.fontColorButton.image = self.swatchWithColor(color: color, size: NSSize(width: 20, height: 20)) } else { self.fontColorButton.image = NSImage(named: "KMImageNameEditPDFColorDisabled") } } else { self.fontColorButton.image = NSImage(named: "KMImageNameEditPDFColorDisabled") } } } 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) { self._trackEvent() 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 { if let cnt = self.handdler?.editingTextAreas.count, cnt <= 1 { 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)) let areas = self.handdler?.editingTextAreas ?? [] if areas.count > 0 { for area in areas { self.listView.setEditingSelectionFontColor(color, with: area) } KMEditPDFPopToolBarWindow.shared.updateFontColor(fontColor: color) } else { KMEditPDFTextManager.manager.setCommonlyFontColor(color: self.currentColor) } self.updatePreviewImage() } @objc func fontBoldAction(_ sender: AnyObject) { let state = self.fontBoldVC?.state ?? .None if state == .Disabled { return } var boldV = false if state == .Act { self.fontBoldVC?.state = .Norm boldV = false } else { self.fontBoldVC?.state = .Act boldV = true } 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, fontStyle: model.fontStyle, fontSize: model.fontSize, type: type, bold: boldV, italic: nil) let model_com = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model_com.fontName, fontStyle: model_com.fontStyle, fontSize: model_com.fontSize, type: .commonly, bold: boldV, italic: nil) self.handdler?.fontBoldAction() self._trackEvent() } @objc func fontItalicAction(_ sender: AnyObject) { let state = self.fontItalicVC?.state ?? .None if state == .Disabled { return } var italicV = false if state == .Act { self.fontItalicVC?.state = .Norm italicV = false } else { self.fontItalicVC?.state = .Act italicV = true } 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,fontStyle: model.fontStyle, fontSize: model.fontSize, type: type, bold: nil, italic: italicV) let model_com = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model_com.fontName, fontStyle: model_com.fontStyle, fontSize: model_com.fontSize, type: .commonly, bold: nil, italic: italicV) self.handdler?.fontItalicAction() self._trackEvent() } @objc func fontAddAction(_ sender: AnyObject) { let state = self.fontAddVC?.state ?? .None if state == .Disabled { return } let size = Float(self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: "") ?? "12") ?? 1 self.fontSizeVC?.stringValue = String(format: "%.0fpt", size+1) self.km_comboBoxSelectionDidChange(self.fontSizeVC!) self._trackEvent() } @objc func fontReduceAction(_ sender: AnyObject) { let state = self.fontReduceVC?.state ?? .None if state == .Disabled { return } let size = Float(self.fontSizeVC?.stringValue.replacingOccurrences(of: "pt", with: "") ?? "12") ?? 1 if size < 1 { return } self.fontSizeVC?.stringValue = String(format: "%.0fpt", size-1) self.km_comboBoxSelectionDidChange(self.fontSizeVC!) self._trackEvent() } @objc func leftAlignmentAction(sender: NSButton?) { self.leftAlignmentVC?.state = .Sel self.rightAlignmentVC?.state = .Norm self.centerAlignmentVC?.state = .Norm self.updateAlignment(alignment: .left) if let _ = sender { KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .left) } self._trackEvent() } @objc func centerAlignmentAction(sender: NSButton?) { self.leftAlignmentVC?.state = .Norm self.rightAlignmentVC?.state = .Norm self.centerAlignmentVC?.state = .Sel self.updateAlignment(alignment: .center) if let _ = sender { KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .center) } self._trackEvent() } @objc func rightAlignmentAction(sender: NSButton?) { self.leftAlignmentVC?.state = .Norm self.rightAlignmentVC?.state = .Sel self.centerAlignmentVC?.state = .Norm self.updateAlignment(alignment: .right) if let _ = sender { KMEditPDFPopToolBarWindow.shared.updateTextAlign(align: .right) } self._trackEvent() } func selectAlignment(alignment: NSTextAlignment) { switch alignment { case .left: self.leftAlignmentAction(sender: nil) case .right: self.rightAlignmentAction(sender: nil) case .center: self.centerAlignmentAction(sender: nil) default: self.leftAlignmentAction(sender: nil) } } @objc func resetTextPresuppositionButtonAction(sender: NSButton) { let state = self.textPresuppositionResetVC?.state ?? .None if state == .Disabled { return } 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(byRangeEdit: areas as? CPDFEditTextArea))) let cfont = self.listView.editingSelectionCFont(byRangeEdit: areas as? CPDFEditTextArea) // let cfont = self.listView.editingSelectionCFont(with: areas as? CPDFEditTextArea) var fontName = cfont?.familyName ?? "Helvetica" // fontName = KMEditPDFTextManager.manager.transformAreaTextFontName(fontName: fontName, fontNames: self.fontNameVC?.items ?? []) // if let data = self.listView?.isBoldCurrentSelection(byRangeEdit: areas as? CPDFEditTextArea), data { // fontName += "-Bold" // } // // if let data = self.listView?.isItalicCurrentSelection(byRangeEdit: areas as? CPDFEditTextArea), data { // fontName += "-Italic" // } // var fontN = fontName // if let data = cfont { // fontN = CPDFFont.convertAppleFont(data) ?? fontName // } 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 { let areas = self.handdler?.editingTextAreas ?? [] if areas.count > 0 { for area in areas { self.listView.setCurrentSelectionAlignment(alignment, with: area) } } 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, style: model.fontStyle, size: size, needChangeListView: needChangeListView) //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) if type == .customize || type == .commonly { self.textPresuppositionResetVC?.state = .Disabled } else { self.textPresuppositionResetVC?.state = .Norm } if needChangeListView { let areas = self.handdler?.editingTextAreas ?? [] for area in areas { self.listView.setCurrentSelectionIsBold(model.bold, with: area) self.listView.setCurrentSelectionIsItalic(model.italic, with: area) } self._boldButtonStateUpdate(fontStyle: "") } } } 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() let win = KMEditPDFPopToolBarWindow.shared if win.isVisible { (win.contentViewController as? KMEditPDFPopToolBarController)?.toolbarView?.reloadData() } } else if obj == fontSizeVC { let size = CGFloat(Float((obj.stringValue.replacingOccurrences(of: "pt", with: "")))!) self.updateFontSize(size: size) self.updatePreviewImage() KMEditPDFPopToolBarWindow.shared.updateFontSizeButtons(enable: true) } else if obj == fontStyleVC { self.updateFontNameAndStyle(name: fontName, style: obj.stringValue) self.updatePreviewImage() } else if obj == self.opacitVC { let stringValue = self.opacitVC?.stringValue ?? "" let value = (stringValue.components(separatedBy: "%").first?.stringToCGFloat() ?? 0) / 100 let view = self.opacityBox.contentView as? KMSliderItemView view?.doubleValue = Double(value) let areas = self.listView.editingAreas() as? [CPDFEditArea] ?? [] for area in areas { self.listView.setOpacityEditArea(area, opacity: value) } } } func km_controlTextDidChange(_ obj: KMDesignSelect) { if obj == self.fontSizeVC { let format = NumberFormatter() format.numberStyle = .decimal var string = obj.stringValue.replacingOccurrences(of: "pt", with: "") string = string.replacingOccurrences(of: "p", with: "") let size = max(Float(string) ?? 1, 1) self.fontSizeVC?.stringValue = format.string(from: NSNumber(value: size)) ?? "" } } func km_controlTextDidEndEditing(_ obj: KMDesignSelect) { if obj == self.fontSizeVC { var string = obj.stringValue.replacingOccurrences(of: "pt", with: "") string = string.replacingOccurrences(of: ",", with: "") let size = Float(string) ?? 1 let sizeValue = max(size, 1) self.fontSizeVC?.stringValue = String(format: "%.0fpt", sizeValue) self.updateFontSize(size: size.cgFloat) } } func km_cellViewWillShow(_ obj: KMDesignSelect, _ cellView: KMBox, _ index: Int) { if obj.isEqual(to: self.textPresuppositionVC) == false { return } if let label = cellView.contentView?.subviews.first as? NSTextField { /* 14.0 "SFProText-Regular" */ let allValues = KMEditPDFTextFontType.allValues() let idx = allValues.count-index-1 if idx == 0 { // custom return } guard let value = allValues.safe_element(for: idx) as? String else { return } let type = KMEditPDFTextFontType.typeOfRawValue(value: value) var fontSize: CGFloat = 0 if type == .h1 { fontSize = 20 } else if type == .h2 { fontSize = 18 } else if type == .h3 { fontSize = 16 } else if type == .b1 { fontSize = 14 } else if type == .b2 { fontSize = 12 } else if type == .b3 { fontSize = 10 } let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: type) let fontName: String = model.fontName // let size: CGFloat = model.fontSize var fontN: String = fontName if let cfont = CPDFFont.mappingFont(withFontString: fontName) { fontN = CPDFFont.convertAppleFont(cfont) ?? fontName } var bold = fontName.contains("Bold") var italic = fontName.contains("Oblique") || fontName.contains("Italic") if bold == false { bold = model.bold } if italic == false { italic = model.italic } // if let data = NSFont(name: fontN, size: fontSidze) { // let attri = NSMutableAttributedString(attributedString: label.attributedStringValue) // attri.addAttribute(.font, value: data, range: NSMakeRange(0, attri.string.count)) // label.attributedStringValue = attri // } else { let attri = NSMutableAttributedString(attributedString: label.attributedStringValue) if bold { attri.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: fontSize), range: NSMakeRange(0, attri.string.count)) } else { attri.addAttribute(.font, value: NSFont.systemFont(ofSize: fontSize), range: NSMakeRange(0, attri.string.count)) } if italic { attri.addAttribute(.obliqueness, value: 0.3, range: NSMakeRange(0, attri.string.count)) } label.attributedStringValue = attri // } } } func km_SelectPopoverWillShow(_ obj: KMDesignSelect) { if obj.isEqual(to: self.fontNameVC) { self._trackEvent() } else if obj.isEqual(to: self.fontStyleVC) { self._trackEvent() } else if obj.isEqual(to: self.fontSizeVC) { self._trackEvent() } } } //MARK: - TextPresupposition extension KMEditPDFTextPropertyViewController { //MARK: 基本属性调整 func updateTextPresupposition(fontName: String, style: 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: fontName, 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: self.fontName,fontStyle: self.fontStyle, 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) var styleString = style let fontStyleNames = CPDFFont.fontNames(forFamilyName: name) if !fontStyleNames.contains(style) { if fontStyleNames.isEmpty { self.fontStyleVC?.addItems(withObjectValues: [style]) self.fontStyleVC?.selectItem(at: 0) // styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue) styleString = style } else { self.fontStyleVC?.addItems(withObjectValues: fontStyleNames) self.fontStyleVC?.selectItem(at: 0) // styleString = KMEditPDFTextManager.manager.checkFontStyle(style: self.fontStyleVC!.stringValue) styleString = fontStyleNames.first ?? "" } } else { self.fontStyleVC?.addItems(withObjectValues: fontStyleNames) self.fontStyleVC?.selectItem(at: fontStyleNames.firstIndex(of: style) ?? 0) } // var fontName = "\(name)-\(styleString)" // if styleString.count == 0 { // fontName = name // } //数据保存返回 if self.selectAreas.count > 0 { if needChangeListView { let areas = self.handdler?.editingTextAreas ?? [] for area in areas { // self.listView.setEditingSelectionFontName(name, with: area) let font = CPDFFont(familyName: name, fontStyle: styleString) self.listView.setEditSelectionCFont(font, with: area) // if styleString.contains("Bold") { // self.listView.setCurrentSelectionIsBold(true, with: area) // } else { // self.listView.setCurrentSelectionIsBold(false, with: area) // } // // if styleString.contains("Oblique") || styleString.contains("Italic") { // self.listView.setCurrentSelectionIsItalic(true, with: area) // } else { // self.listView.setCurrentSelectionIsItalic(false, with: area) // } } } } else { if needSave { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: name, fontStyle: styleString, fontSize: model.fontSize, type: .commonly, bold: nil, italic: nil) } } self.fontName = name // self.fontStyle = self.fontStyleVC?.stringValue ?? "Regular" self.fontStyle = styleString 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: name, fontStyle: styleString, fontSize: model.fontSize, type: type, bold: nil, italic: nil) } //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) // } self._boldButtonStateUpdate(fontStyle: styleString) } 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 { let areas = self.handdler?.editingTextAreas ?? [] if areas.count > 0 { if needChangeListView { for area in areas { self.listView.setEditingSelectionFontSize(size, with: area, isAutoSize: false) } } } else { if needSave { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) KMEditPDFTextManager.manager.changeTextPresupposition(fontName: model.fontName, fontStyle: model.fontStyle,fontSize: size, type: .commonly, bold: nil, italic: nil) } } 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, fontStyle: model.fontStyle, fontSize: size, type: type, bold: nil, italic: nil) } //刷新样式 textPresuppositionVC?.addItems(withObjectValues: KMEditPDFTextManager.manager.updateTextPresuppositionFontNameArray()) textPresuppositionVC?.selectItem(at: textPresuppositionVC!.indexOfSelectedItem) } } // Bold private func _boldButtonStateUpdate(fontStyle fStyle: String) { if self.selectAreas.count == 1 { let areas = self.selectAreas.first if areas is CPDFEditTextArea { let area = areas as! CPDFEditTextArea if let value = self.listView?.isBoldCurrentSelection(byRangeEdit: area) { self.fontBoldVC?.state = value ? .Act : .Norm } if let value = self.listView?.isItalicCurrentSelection(byRangeEdit: area) { self.fontItalicVC?.state = value ? .Act : .Norm } } } else if self.selectAreas.count > 1 { if let data = self.handdler?.editAreasFontBoldIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let value = self.listView.isBoldCurrentSelection(byRangeEdit: area) self.fontBoldVC?.state = value ? .Act : .Norm } else { self.fontBoldVC?.state = .Disabled } } else { self.fontBoldVC?.state = .Disabled } if let data = self.handdler?.editAreasFontItalicIsEqual(), data { if let area = self.handdler?.editingTextAreas.first { let value = self.listView.isItalicCurrentSelection(byRangeEdit: area) self.fontItalicVC?.state = value ? .Act : .Norm } else { self.fontItalicVC?.state = .Disabled } } else { self.fontItalicVC?.state = .Disabled } } else { let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly) self.fontBoldVC?.state = model.bold ? .Act : .Norm self.fontItalicVC?.state = model.italic ? .Act : .Norm } } //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"] } private func _trackEvent() { kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditText":"SubTbr_EditText_PropertiesPanel_All"]) } private func _trackAlignEvent() { kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditText":"SubTbr_EditContent_AlignContent"]) } }