Просмотр исходного кода

【2025】【Edit】文字编辑完善

niehaoyu 3 дней назад
Родитель
Сommit
17b10a34fb

+ 33 - 15
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Edit.swift

@@ -25,7 +25,10 @@ extension CPDFListView {
     func km_editAreasOpacitys(_ areas: [CPDFEditArea]) -> [CGFloat] {
         var arr: [CGFloat] = []
         for area in areas {
-            let data = self.opacityByRange(for: area)
+            var data = self.opacityByRange(for: area)
+            if data < 0 {
+               data = self.opacity(for: area)
+            }
             arr.append(data)
         }
         return arr
@@ -67,8 +70,14 @@ extension CPDFListView {
     func km_editTextAreasFontColors(_ textAreas: [CPDFEditTextArea]) -> [NSColor] {
         var datas: [NSColor] = []
         for area in textAreas {
-            if let data = self.editingSelectionFontColor(with: area) {
-                datas.append(data)
+            if let selection = area.selection, let string = selection.string(), string.isEmpty == false {
+                if let data = self.editingSelectionFontColor(byRangeEdit: area) {
+                    datas.append(data)
+                }
+            } else {
+                if let data = self.editingSelectionFontColor(with: area) {
+                    datas.append(data)
+                }
             }
         }
         return datas
@@ -180,7 +189,16 @@ extension CPDFListView {
 
 //MARK: - Action
 extension CPDFListView {
-    func fontColorAction(color: NSColor?) {
+    
+    func setEditingAreasOpacity(_ opacity: CGFloat) {
+        let areas = self.km_EditingAreas()
+        for area in areas {
+            self.setOpacityEditArea(area, opacity: opacity)
+        }
+    }
+ 
+    //MARK: -Text
+    func changeEditingTextarea_Color(color: NSColor?) {
         guard let theColor = color else {
             return
         }
@@ -190,13 +208,19 @@ extension CPDFListView {
         }
     }
     
-    func fontStyleAction(fontName: String?) {
-        guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
-            return
-        }
+    func setEditingTextarea_font(font: CPDFFont) {
         let areas = self.km_editingTextAreas()
         for area in areas {
             self.setEditSelectionCFont(font, with: area)
+            let cFont = self.editingSelectionCFont(with: area)
+            print("1")
+        }
+    }
+    
+    func setEditingTextarea_FontSize(size: CGFloat) {
+        let areas = self.km_editingTextAreas()
+        for area in areas {
+            self.setEditingSelectionFontSize(size, with: area, isAutoSize: false)
         }
     }
     
@@ -240,6 +264,7 @@ extension CPDFListView {
         }
     }
     
+    //MARK: -Image
     func leftRotateAction() {
         let areas = self.km_editingImageAreas()
         for area in areas {
@@ -280,13 +305,6 @@ extension CPDFListView {
         }
     }
     
-    func setEditingAreasOpacity(_ opacity: CGFloat) {
-        let areas = self.km_EditingAreas()
-        for area in areas {
-            self.setOpacityEditArea(area, opacity: opacity)
-        }
-    }
- 
     //MARK: -Exprot 导出
     func exportEditingImageAreasAction(format: String) {
         let areas = self.km_editingImageAreas()

+ 146 - 16
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/EditPDF/KMEditPDFTextPropertyViewController.swift

@@ -31,11 +31,7 @@ class KMEditPDFTextPropertyViewController: NSViewController {
      
     private var familyNames = CPDFFont.familyNames
      
-    var pdfView: CPDFListView? {
-        didSet {
-            reloadData()
-        }
-    }
+    var pdfView: CPDFListView?
      
     var currentArea: CPDFEditTextArea?
     
@@ -70,6 +66,7 @@ class KMEditPDFTextPropertyViewController: NSViewController {
             }
             fontTitleSelect.updateMenuItemsArr(menuItemArr)
         }
+        fontTitleSelect.delegate = self
 
         fontTitleButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_more"))
         fontTitleButton.setTarget(self, action: #selector(buttonClicked(_:)))
@@ -96,7 +93,8 @@ class KMEditPDFTextPropertyViewController: NSViewController {
                                                               state: .normal,
                                                               creatable: true,
                                                               text: "12 pt",
-                                                              regexString: "0123456789 pt")
+                                                              textUnit: " pt",
+                                                              regexString: "0123456789")
         if true {
             var sizeItemArr: [ComponentMenuitemProperty] = []
             for string in KMHeaderFooterManager.getFontSize() {
@@ -131,6 +129,7 @@ class KMEditPDFTextPropertyViewController: NSViewController {
         colorGroup.delegate = self
         
         colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
+        colorSlider.delegate = self
         
         colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
                                                                         state: .normal,
@@ -160,15 +159,87 @@ class KMEditPDFTextPropertyViewController: NSViewController {
         }
         
         if let area = currentArea {
-            let fontName = pdfView?.km_editTextAreasFontNames([area])
-            let fontStyle = pdfView?.km_editTextAreasFontStyles([area])
-            let fontSize = pdfView?.km_editTextAreasFontSizes([area])
-            let bold = pdfView?.km_editTextAreasFontBolds([area])
-            let itality = pdfView?.km_editTextAreasFontItalics([area])
-            let alignment = pdfView?.km_editTextAreasTextAlignments([area])
-            let colors = pdfView?.km_editTextAreasFontColors([area])
-            let opacity = pdfView?.km_editAreasOpacitys([area])
-            print("1")
+            if let fontNames = pdfView?.km_editTextAreasFontNames([area]) {
+                if let fontName = fontNames.first {
+                    fontNameSelect.properties.text = fontName
+                    fontNameSelect.reloadData()
+                }
+            }
+            
+            if let fontStyles = pdfView?.km_editTextAreasFontStyles([area]) {
+                var styles: [String] = []
+                if let fontStyle = fontStyles.first, fontStyle.isEmpty == false {
+                    styles = fontStyles
+                    
+                    fontStyleSelect.properties.text = fontStyle
+                } else {
+                    if let name = fontNameSelect.properties.text {
+                        styles = CPDFFont.fontNames(forFamilyName: name)
+                        if styles.isEmpty == true {
+                            styles.append("Regular")
+                            fontStyleSelect.properties.text = "Regular"
+                        }
+                    }
+                }
+                fontStyleSelect.reloadData()
+                
+                var menuItemArr: [ComponentMenuitemProperty] = []
+                for string in styles {
+                    let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
+                    menuItemArr.append(item)
+                }
+                fontStyleSelect.updateMenuItemsArr(menuItemArr)
+                
+                
+            }
+            
+            if let fontSizes = pdfView?.km_editTextAreasFontSizes([area]) {
+                if let fontSize = fontSizes.first {
+                    
+                }
+            }
+            
+            if let bolds = pdfView?.km_editTextAreasFontBolds([area]) {
+                if let bold = bolds.first {
+                    
+                }
+            }
+            
+            if let italitys = pdfView?.km_editTextAreasFontItalics([area]) {
+                if let itality = italitys.first {
+                    
+                }
+                
+            }
+           
+            
+            if let alignments = pdfView?.km_editTextAreasTextAlignments([area]) {
+                if let alignment = alignments.first {
+                    
+                }
+            }
+           
+            if let colors = pdfView?.km_editTextAreasFontColors([area]) {
+                if let color = colors.first {
+                    colorGroup.currentColor = color
+                } else {
+                    colorGroup.currentColor = NSColor.clear
+                }
+                colorGroup.refreshUI()
+            }
+         
+            if let opacitys = pdfView?.km_editAreasOpacitys([area]) {
+                if var opacity = opacitys.first {
+                    opacity = min(1, opacity)
+                    opacity = max(0, opacity)
+                    
+                    colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
+                    colorOpacitySelect.reloadData()
+                    
+                    colorSlider.properties.percent = opacity
+                    colorSlider.reloadData()
+                }
+            }
         }
         
         
@@ -182,10 +253,69 @@ class KMEditPDFTextPropertyViewController: NSViewController {
 }
 
 
+//MARK: - ComponentSelectDelegate
 extension KMEditPDFTextPropertyViewController: ComponentSelectDelegate {
+    func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
+        if view == fontTitleSelect {
+            
+        } else if view == fontNameSelect {
+            if let fontName = menuItemProperty?.text {
+                var cFont = CPDFFont.mappingFont(withFontString: fontName)
+                var styles = CPDFFont.fontNames(forFamilyName: fontName)
+                if styles.isEmpty == false, let style = styles.first {
+                    cFont = CPDFFont.init(familyName: fontName, fontStyle: style)
+                }
+                if let cFont = cFont {
+                    pdfView?.setEditingTextarea_font(font: cFont)
+                }
+            }
+        } else if view == fontStyleSelect {
+            
+        } else if view == fontSizeSelect {
+            if let text = menuItemProperty?.text {
+                let result = text.stringByDeleteCharString(" pt")
+                let fontSize = result.stringToCGFloat()
+                pdfView?.setEditingTextarea_FontSize(size: fontSize)
+            }
+        }
+        reloadData()
+        
+    }
     
+    func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
+        if view == fontSizeSelect {
+            if let string = view.properties.text {
+                let result = string.stringByDeleteCharString(" pt")
+                let size = result.stringToCGFloat()
+                let fontSize = max(size, 100)
+                
+                pdfView?.setEditingTextarea_FontSize(size: fontSize)
+                
+            }
+        }
+        reloadData()
+         
+    }
+
 }
 
+//MARK: - ComponentCColorDelegate
 extension KMEditPDFTextPropertyViewController: ComponentCColorDelegate {
-    
+    func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
+        if view == colorGroup {
+            pdfView?.changeEditingTextarea_Color(color: color)
+        }
+    }
+}
+
+//MARK: - ComponentSliderDelegate
+extension KMEditPDFTextPropertyViewController: ComponentSliderDelegate {
+    func componentSliderDidUpdate(_ view: ComponentSlider) {
+        let percent = view.properties.percent
+        
+        self.pdfView?.setEditingAreasOpacity(percent)
+        
+        reloadData()
+        
+    }
 }