123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- //
- // CPDFPDFInsertViewController.m
- // compdfkit-tools
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- #import "CPDFPDFInsertViewController.h"
- #import "CPDFColorUtils.h"
- #import "CInsertBlankPageCell.h"
- #import "CBlankPageModel.h"
- #import <ComPDFKit/ComPDFKit.h>
- @interface CPDFPDFInsertViewController () <UITableViewDelegate, UITableViewDataSource, CInsertBlankPageCellDelegate>
- @property (nonatomic, strong) UIButton *cancelBtn;
- @property (nonatomic, strong) UIButton *saveButton;
- @property (nonatomic, strong) UIView *headerView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) NSArray *dataArray;
- @property (nonatomic, assign) BOOL isSelect;
- @property (nonatomic, strong) CPDFDocument *document;
- @property (nonatomic, strong) UIButton *selectRangeBtn;
- @property (nonatomic, strong) CInsertBlankPageCell *rangePreCell;
- @property (nonatomic, strong) UIButton *selectLocationBtn;
- @property (nonatomic, strong) CInsertBlankPageCell *locationPreCell;
- @property (nonatomic, strong) NSMutableArray *pageLoactionBtns;
- @property (nonatomic, strong) NSMutableArray *pageRangeBtns;
- @property (nonatomic, strong) CBlankPageModel *pageModel;
- @property (nonatomic, strong) UITextField *locationTextField;
- @property (nonatomic, strong) UITextField *rangeTextField;
- @property (nonatomic, strong) UIScrollView *scrcollView;
- @end
- @implementation CPDFPDFInsertViewController
- #pragma mark - Initializers
- - (instancetype)initWithDocument:(CPDFDocument *)document {
- if (self = [super init]) {
- self.document = document;
- }
- return self;
- }
- #pragma mark - Accessors
- - (NSArray *)dataArray {
- if (!_dataArray) {
- NSArray *dataArray = @[NSLocalizedString(@"Font Name", nil), NSLocalizedString(@"Page Range", nil), NSLocalizedString(@"All Page", nil), NSLocalizedString(@"Odd number", nil), NSLocalizedString(@"Even number", nil), NSLocalizedString(@"Specify page", nil), NSLocalizedString(@"for example:1,3-5,10", nil), NSLocalizedString(@"Page Location", nil),NSLocalizedString(@"Home page", nil), NSLocalizedString(@"Last page", nil), NSLocalizedString(@"Insert before specifiled page", nil), NSLocalizedString(@"Please enter a page", nil), NSLocalizedString(@"Insert after specifiled page", nil)];
- _dataArray = dataArray;
- }
- return _dataArray;
- }
- #pragma mark - ViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.headerView = [[UIView alloc] init];
- self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
- self.headerView.layer.borderWidth = 1.0;
- self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
- [self.view addSubview:self.headerView];
-
- self.titleLabel = [[UILabel alloc] init];
- self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
- self.titleLabel.text = NSLocalizedString(@"Insert PDF page", nil);
- self.titleLabel.textAlignment = NSTextAlignmentCenter;
- self.titleLabel.font = [UIFont systemFontOfSize:20];
- self.titleLabel.adjustsFontSizeToFitWidth = YES;
- [self.headerView addSubview:self.titleLabel];
-
- self.saveButton = [[UIButton alloc] init];
- self.saveButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
- [self.saveButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
- [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
- [self.saveButton addTarget:self action:@selector(buttonItemClicked_save:) forControlEvents:UIControlEventTouchUpInside];
- [self.headerView addSubview:self.saveButton];
-
- self.cancelBtn = [[UIButton alloc] init];
- self.cancelBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- self.cancelBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
- [self.cancelBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
- [self.cancelBtn setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
- [self.cancelBtn addTarget:self action:@selector(buttonItemClicked_cancel:) forControlEvents:UIControlEventTouchUpInside];
- [self.headerView addSubview:self.cancelBtn];
-
- self.scrcollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)];
- self.scrcollView.scrollEnabled = YES;
- self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 800);
- self.scrcollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- self.scrcollView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
- [self.view addSubview:self.scrcollView];
-
- self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- self.tableView.rowHeight = 60.0;
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
- [self.scrcollView addSubview:self.tableView];
-
- self.isSelect = NO;
- self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
-
- self.pageModel = [[CBlankPageModel alloc] init];
- self.pageModel.pageIndex = 0;
- self.pageModel.rotation = 0;
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- for (int i = 0; i < self.document.pageCount; i++) {
- [indexSet addIndex:i];
- }
- self.pageModel.indexSet = indexSet;
-
- self.pageLoactionBtns = [NSMutableArray array];
- self.pageRangeBtns = [NSMutableArray array];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardwillChangeFrame:)
- name:UIKeyboardWillChangeFrameNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillHide:)
- name:UIKeyboardWillHideNotification
- object:nil];
- }
- - (void)viewWillLayoutSubviews {
- [super viewWillLayoutSubviews];
- self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
- self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 200)/2, 0, 200, 50);
- self.scrcollView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50);
-
- if (@available(iOS 11.0, *)) {
- self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 40);
- self.cancelBtn.frame = CGRectMake( self.view.safeAreaInsets.left+20, 5, 50, 40);
- } else {
- self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 40);
- self.cancelBtn.frame = CGRectMake(20, 5, 50, 40);
- }
- }
- #pragma mark - Private Methods
- - (void)popoverWarning {
- UIAlertAction *OKAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction *action) {
- }];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil)
- message:NSLocalizedString(@"Sorry,out of page scope", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:OKAction];
- [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 {
- [self dismissViewControllerAnimated:YES completion:^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(pdfInsertViewControllerSave:pageModel:)]) {
- [self.delegate pdfInsertViewControllerSave:self pageModel:self.pageModel];
- }
- }];
- }
- - (void)buttonItemClicked_cancel:(id)sender {
- [self dismissViewControllerAnimated:YES completion:^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(pdfInsertViewControllerCancel:)]) {
- [self.delegate pdfInsertViewControllerCancel:self];
- }
- }];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.dataArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- CInsertBlankPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pageCell"];
- if (cell == nil) {
- cell = [[CInsertBlankPageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"pageCell"];
- cell.delegate = self;
- }
- switch (indexPath.row) {
- case 0:
- {
- [cell setCellStyle:CInsertBlankPageCellSize label:self.dataArray[indexPath.row]];
- CGFloat maxLength = 200.0;
- UIFont *font = [UIFont systemFontOfSize:18.0];
- cell.sizeLabel.text = [self stringByTruncatingMiddleWithFont:font maxLength:maxLength];
- }
- break;
- case 1:
- [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
- break;
- case 2:
- {
- [cell setCellStyle:CInsertBlankPageCellRangeSelect label:self.dataArray[indexPath.row]];
- if (![self.pageRangeBtns containsObject:cell.rangeSelectBtn]) {
- [self.pageRangeBtns addObject:cell.rangeSelectBtn];
- }
-
- self.rangePreCell = cell;
- cell.rangeSelectBtn.selected = !cell.rangeSelectBtn.selected;
- cell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 3 ... 5:
- {
- [cell setCellStyle:CInsertBlankPageCellRangeSelect label:self.dataArray[indexPath.row]];
- if (![self.pageRangeBtns containsObject:cell.rangeSelectBtn]) {
- [self.pageRangeBtns addObject:cell.rangeSelectBtn];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 6:
- [cell setCellStyle:CInsertBlankPageCellRangeTextFiled label:self.dataArray[indexPath.row]];
- break;
- case 7:
- [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
- break;
- case 8:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
- if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
- [self.pageLoactionBtns addObject:cell.locationSelectBtn];
- }
-
- if (!self.currentPageIndex) {
- self.locationPreCell = cell;
- cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
- cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 9:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
- if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
- [self.pageLoactionBtns addObject:cell.locationSelectBtn];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 10:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
- if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
- [self.pageLoactionBtns addObject:cell.locationSelectBtn];
- }
- if (self.currentPageIndex) {
- self.locationPreCell = cell;
- cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
- cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 11:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationTextFiled label:self.dataArray[indexPath.row]];
- if (self.currentPageIndex) {
- cell.locationTextField.text = [NSString stringWithFormat:@"%lu", self.currentPageIndex];
- self.pageModel.pageIndex = self.currentPageIndex-1;
- }
- self.locationTextField = cell.locationTextField;
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 12:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
- if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
- [self.pageLoactionBtns addObject:cell.locationSelectBtn];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
-
- default:
- break;
- }
-
- return cell;
- }
- #pragma mark - CInsertBlankPageCellDelegate
- - (void)insertBlankPageCellRange:(CInsertBlankPageCell *)insertBlankPageCell button:(UIButton *)buttom {
- if (self.rangePreCell) {
- self.rangePreCell.rangeSelectBtn.selected = !self.rangePreCell.rangeSelectBtn.selected;
- self.rangePreCell.rangeSelectLabel.textColor = [UIColor grayColor];
- if (self.rangePreCell.rangeSelectBtn != buttom) {
- buttom.selected = !buttom.selected;
- insertBlankPageCell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- self.rangePreCell = insertBlankPageCell;
- } else {
- self.rangePreCell.rangeSelectLabel.textColor = [UIColor grayColor];
- self.rangePreCell = nil;
- }
- } else {
- self.rangePreCell = insertBlankPageCell;
- buttom.selected = !buttom.selected;
- insertBlankPageCell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- }
-
- NSInteger range = [self.pageRangeBtns indexOfObject:buttom];
- switch (range) {
- case 0:
- {
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- for (int i = 0; i < self.document.pageCount; i++) {
- [indexSet addIndex:i];
- }
- self.pageModel.indexSet = indexSet;
- self.rangeTextField.userInteractionEnabled = NO;
- }
- break;
- case 1:
- {
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- for (int i = 0; i < self.document.pageCount; i += 2) {
- [indexSet addIndex:i];
- }
- self.pageModel.indexSet = indexSet;
- self.rangeTextField.userInteractionEnabled = NO;
- }
- break;
- case 2:
- {
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- for (int i = 1; i < self.document.pageCount; i += 2) {
- [indexSet addIndex:i];
- }
- self.pageModel.indexSet = indexSet;
- self.rangeTextField.userInteractionEnabled = NO;
- }
- break;
- case 4:
- self.rangeTextField.userInteractionEnabled = YES;
- break;
-
- default:
- break;
- }
- }
- - (void)insertBlankPageCellLocation:(CInsertBlankPageCell *)insertBlankPageCell button:(UIButton *)buttom {
- if (self.locationPreCell) {
- self.locationPreCell.locationSelectBtn.selected = !self.locationPreCell.locationSelectBtn.selected;
- self.locationPreCell.locationSelectLabel.textColor = [UIColor grayColor];
- if (self.locationPreCell.locationSelectBtn != buttom) {
- buttom.selected = !buttom.selected;
- insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- self.locationPreCell = insertBlankPageCell;
- } else {
- self.locationPreCell.locationSelectLabel.textColor = [UIColor grayColor];
- self.locationPreCell = nil;
- }
- } else {
- self.locationPreCell = insertBlankPageCell;
- buttom.selected = !buttom.selected;
- insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- }
-
- NSInteger location = [self.pageLoactionBtns indexOfObject:buttom];
- switch (location) {
- case 0:
- self.pageModel.pageIndex = 0;
- self.locationTextField.userInteractionEnabled = NO;
- break;
- case 1:
- self.pageModel.pageIndex = -2;
- self.locationTextField.userInteractionEnabled = NO;
- break;
- case 2:
- self.pageModel.pageIndex = self.pageModel.pageIndex - 1;
- self.locationTextField.userInteractionEnabled = YES;
- break;
- case 3:
- self.pageModel.pageIndex = self.pageModel.pageIndex + 1;
- self.locationTextField.userInteractionEnabled = YES;
- break;
-
- default:
- break;
- }
- }
- - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell pageRange:(NSString *)pageRange {
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- if ([pageRange containsString:@","]) {
- NSArray<NSString *> *pageIndexsArray = [pageRange componentsSeparatedByString:@","];
- for (NSString *pageIndexStr in pageIndexsArray) {
- if ([pageIndexStr containsString:@"-"]) {
- NSArray<NSString *> *pageIndexs = [pageIndexStr componentsSeparatedByString:@"-"];
- NSInteger start = [pageIndexs[0] integerValue];
- NSInteger end = [pageIndexs[1] integerValue];
- if (end > self.document.pageCount-1) {
- [self popoverWarning];
- } else {
- for (NSInteger i = start; i <= end; i++) {
- [indexSet addIndex:i];
- }
- }
- } else {
- if ([pageIndexStr integerValue] > self.document.pageCount-1) {
- [self popoverWarning];
- } else {
- [indexSet addIndex:[pageIndexStr integerValue]];
- }
- }
- }
- } else {
- if ([pageRange containsString:@"-"]) {
- NSArray<NSString *> *pageIndexs = [pageRange componentsSeparatedByString:@"-"];
- NSInteger start = [pageIndexs[0] integerValue];
- NSInteger end = [pageIndexs[1] integerValue];
- if (end > self.document.pageCount-1) {
- [self popoverWarning];
- } else {
- for (NSInteger i = start; i <= end; i++) {
- [indexSet addIndex:i];
- }
- }
- } else {
- if ([pageRange integerValue] > self.document.pageCount-1) {
- [self popoverWarning];
- } else {
- [indexSet addIndex:[pageRange integerValue]];
- }
- }
- }
-
- self.pageModel.indexSet = indexSet;
- }
- - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell pageIndex:(NSInteger)pageIndex {
- self.pageModel.pageIndex = pageIndex;
- }
- #pragma mark - NSNotification
- - (void)keyboardwillChangeFrame:(NSNotification *)notification {
- NSDictionary *userInfo = [notification userInfo];
- NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
- CGRect frame = value.CGRectValue;
- CGRect rect = [self.locationTextField convertRect:self.locationTextField.frame toView:self.view];
- if(CGRectGetMaxY(rect) > self.view.frame.size.height - frame.size.height) {
- UIEdgeInsets insets = self.tableView.contentInset;
- insets.bottom = frame.size.height + self.locationTextField.frame.size.height;
- self.tableView.contentInset = insets;
- }
- }
- - (void)keyboardWillHide:(NSNotification *)notification {
- UIEdgeInsets insets = self.tableView.contentInset;
- insets.bottom = 0;
- self.tableView.contentInset = insets;
- }
- @end
|