瀏覽代碼

Merge branch 'compdfkit_demo_ios' of git.kdan.cc:others/kmpdfkit_demo into compdfkit_demo_ios

chenyu 1 年之前
父節點
當前提交
baa9be8ab3

+ 2 - 0
PageEdit-Ctrl-Demo/PageEdit-Ctrl-Demo/CPDFViewController.m

@@ -74,6 +74,8 @@
     [weakSelf reloadDocumentWithFilePath:self.filePath password:nil completion:^(BOOL result) {
         [weakSelf.pdfListView reloadInputViews];
     }];
+    
+    [weakSelf.pdfListView reloadInputViews];
 }
 
 - (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex {

+ 1 - 0
compdfkit-tools/compdfkit-tools/Common/Controls/PDFViewController/CPDFViewBaseController.m

@@ -478,6 +478,7 @@
             }
             NSURL *url = [NSURL fileURLWithPath:documentPath];
             CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
+            self.filePath = documentPath;
             
             if (document.error && document.error.code != CPDFDocumentPasswordError) {
                 UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

+ 19 - 1
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPDFInsertViewController.m

@@ -179,6 +179,22 @@
     [self presentViewController:alert animated:YES completion:nil];
 }
 
+- (NSString *)stringByTruncatingMiddleWithFont:(UIFont *)font maxLength:(CGFloat)maxLength {
+    if ([[self.document.documentURL lastPathComponent] sizeWithAttributes:@{NSFontAttributeName: font}].width <= maxLength) {
+        return [self.document.documentURL lastPathComponent];
+    }
+
+    NSInteger halfLength = ([self.document.documentURL lastPathComponent].length) / 4;
+    
+    NSString *firstHalf = [[self.document.documentURL lastPathComponent] substringToIndex:halfLength];
+    
+    NSString *secondHalf = [[self.document.documentURL lastPathComponent] substringFromIndex:[self.document.documentURL lastPathComponent].length - halfLength];
+    
+    NSString *truncatedStr = [NSString stringWithFormat:@"%@...%@", firstHalf, secondHalf];
+    
+    return truncatedStr;
+}
+
 #pragma mark - Action
 
 - (void)buttonItemClicked_save:(id)sender {
@@ -213,7 +229,9 @@
         case 0:
         {
             [cell setCellStyle:CInsertBlankPageCellSize label:self.dataArray[indexPath.row]];
-            cell.sizeLabel.text = [self.document.documentURL lastPathComponent];
+            CGFloat maxLength = 200.0;
+            UIFont *font = [UIFont systemFontOfSize:18.0];
+            cell.sizeLabel.text = [self stringByTruncatingMiddleWithFont:font maxLength:maxLength];
         }
             break;
         case 1:

+ 69 - 10
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageEditViewController.m

@@ -256,6 +256,65 @@
     }
 }
 
+- (NSString *)fileNameWithSelectedPages {
+    NSArray * selectPages = [self selectedPages];
+    NSString *fileName = nil;
+    if (selectPages.count > 0) {
+        if (selectPages.count == 1) {
+            NSInteger idx = [self.pdfView.document indexForPage:selectPages.firstObject] + 1;
+            return [NSString stringWithFormat:@"%ld",idx];
+        }
+        
+        NSMutableSet *sortIndex = [NSMutableSet set];
+        for (CPDFPage * page in selectPages) {
+            NSInteger idx = [self.pdfView.document indexForPage:page] + 1;
+            [sortIndex addObject:@(idx)];
+        }
+        NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
+        NSArray *sortDesc = @[sort];
+        NSArray *sortArray = [sortIndex sortedArrayUsingDescriptors:sortDesc];
+        
+        NSInteger a = 0;
+        NSInteger b = 0;
+        
+        for (NSNumber *num in sortArray) {
+            if (fileName) {
+                if (num.integerValue == b+1) {
+                    b = num.integerValue;
+                    if (num == sortArray.lastObject) {
+                        fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@"%ld-%ld",a,b]];
+                    }
+                } else {
+                    if (a == b) {
+                        fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@"%ld,",a]];
+                    } else {
+                        fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@"%ld-%ld,",a,b]];
+                    }
+                    a = b = num.integerValue;
+                    if (num == sortArray.lastObject) {
+                        fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@"%ld",a]];
+                    }
+                }
+            } else {
+                fileName = @"";
+                a = b = num.integerValue;
+            }
+        }
+        return fileName;
+    }
+    return @"";
+}
+
+- (NSArray<CPDFPage *> *)selectedPages {
+    NSMutableArray *pages = [NSMutableArray array];
+    [[self.collectionView indexPathsForSelectedItems] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        if (idx < self.pdfView.document.pageCount) {
+            [pages addObject:[self.pdfView.document pageAtIndex:obj.item]];
+        }
+    }];
+    return pages;
+}
+
 #pragma mark - GestureRecognized
 
 - (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
@@ -396,7 +455,7 @@
 - (void)pageEditToolBarExtract:(CPageEditToolBar *)pageEditToolBar {
     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
     NSString *fileName = self.pdfView.document.documentURL.lastPathComponent.stringByDeletingPathExtension;
-    NSString *filePath = [NSString stringWithFormat:@"%@/%@_Pages.pdf",path,fileName];
+    NSString *filePath = [NSString stringWithFormat:@"%@/%@_%@.pdf",path,fileName,[self fileNameWithSelectedPages]];
     NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
     for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
         [indexSet addIndex:indexPath.item];
@@ -412,16 +471,16 @@
         activityVC.popoverPresentationController.sourceView = (UIButton *)self.pageEditToolBar.pageEditBtns[2];
         activityVC.popoverPresentationController.sourceRect = ((UIButton *)self.pageEditToolBar.pageEditBtns[2]).bounds;
     }
-        [self presentViewController:activityVC animated:YES completion:nil];
-        activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
+    [self presentViewController:activityVC animated:YES completion:nil];
+    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
+
+        if (completed) {
+            NSLog(@"Success!");
+        } else {
+            NSLog(@"Failed Or Canceled!");
+        }
+    };
 
-            if (completed) {
-                NSLog(@"Success!");
-            } else {
-                NSLog(@"Failed Or Canceled!");
-            }
-        };
-    
     [self.pageEditToolBar reloadData];
     
     self.isPageEdit = YES;

+ 3 - 1
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageInsertViewController.m

@@ -232,7 +232,9 @@
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
-            cell.locationSelectBtn.selected = YES;
+            self.preCell = cell;
+            cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
+            cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
             cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;

+ 6 - 5
compdfkit-tools/compdfkit-tools/PageEdit/Views/CInsertBlankPageCell.m

@@ -189,12 +189,13 @@
 #pragma mark - Private Methods
 
 - (UIView *)sizeSelectViewCreate {
-    UIView *tSelectView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 120, 50)];
-    self.sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
-    self.sizeLabel.adjustsFontSizeToFitWidth = YES;
+    UIView *tSelectView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, self.bounds.size.width-self.textLabel.bounds.size.width, 50)];
+    self.sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width-self.textLabel.bounds.size.width-40, 50)];
+    self.sizeLabel.textAlignment = NSTextAlignmentRight;
+    self.sizeLabel.font = [UIFont systemFontOfSize:18];
     self.sizeLabel.text = NSLocalizedString(@"A4 (210 X 297mm)", nil);
     
-    self.sizeSelectBtn = [[UIButton alloc] initWithFrame:CGRectMake(80, 0, 40, 50)];
+    self.sizeSelectBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width-self.textLabel.bounds.size.width-40, 0, 40, 50)];
     self.sizeSelectBtn.selected = NO;
     [self.sizeSelectBtn setImage:[UIImage imageNamed:@"CInsertBlankPageCellSelectDown" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
     [self.sizeSelectBtn setImage:[UIImage imageNamed:@"CInsertBlankPageCellSelect" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
@@ -266,9 +267,9 @@
 #pragma mark - Action
 
 - (void)buttonItemClicked_size:(UIButton *)button {
-    self.sizeSelectBtn.selected = !self.sizeSelectBtn.selected;
     
     if (self.delegate && [self.delegate respondsToSelector:@selector(insertBlankPageCell:isSelect:)]) {
+        self.sizeSelectBtn.selected = !self.sizeSelectBtn.selected;
         [self.delegate insertBlankPageCell:self isSelect:self.sizeSelectBtn.selected];
     }
 }