Browse Source

综合 - 修复KMDesignSelect下拉框宽度不够问题

wanjun 2 years ago
parent
commit
aad6d36bc4

+ 8 - 1
PDF Office/PDF Office/Class/Home/Category/String+KMExtensions.swift

@@ -11,7 +11,7 @@ extension String {
     var fileURL: URL {
         return URL(fileURLWithPath: self)
     }
-
+    
     // 获取路径字符串后缀
     var `extension`: String {
         if let index = self.lastIndex(of: ".") {
@@ -87,4 +87,11 @@ extension String {
         let urlTest: NSPredicate =  NSPredicate(format: "SELF MATCHES %@", urlRegex)
         return urlTest.evaluate(with: url)
     }
+    
+    // MARK: 获取文字宽高
+    
+    func getTextRectSize(font:NSFont, size:CGSize) -> CGSize {
+        let rect = self.boundingRect(with: size, options: [.usesLineFragmentOrigin, .usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font: font], context: nil)
+        return rect.size
+    }
 }

+ 8 - 1
PDF Office/PDF Office/Class/Home/ViewController/customViewController/KMDesignSelect.swift

@@ -401,8 +401,15 @@ class KMSelectCell: NSTextFieldCell {
         createFilePopover.behavior = .semitransient
         createFilePopover.setValue(true, forKey: "shouldHideAnchor")
         createFilePopover.show(relativeTo: CGRect(x: view.bounds.origin.x, y: 10, width: view.bounds.size.width, height: view.bounds.size.height), of: view, preferredEdge: .minY)
+        var width = mainBox.frame.width
+        for i in items {
+            let w = i.getTextRectSize(font: .systemFont(ofSize: 14.0), size: CGSize(width: CGFloat(MAXFLOAT), height: 32.0)).width+12*2
+            if width < w {
+                width = w
+            }
+        }
         
-        vc.customBoxWidthLayoutConstraint.constant = mainBox.frame.width
+        vc.customBoxWidthLayoutConstraint.constant = width
         vc.downCallback = {(downEntered: Bool, count: String) -> Void in
             if downEntered {
                 self.stringValue = count