123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- //
- // CPDFPageInsertViewController.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 "CPDFPageInsertViewController.h"
- #import "CPDFColorUtils.h"
- #import "CInsertBlankPageCell.h"
- #import "CBlankPageModel.h"
- @interface CPDFPageInsertViewController () <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) NSMutableArray *pageLoactionBtns;
- @property (nonatomic, strong) CBlankPageModel *pageModel;
- @property (nonatomic, strong) UITextField *locationTextField;
- @property (nonatomic, strong) CInsertBlankPageCell *preCell;
- @end
- @implementation CPDFPageInsertViewController
- #pragma mark - Accessors
- - (NSArray *)dataArray {
- if (!_dataArray) {
- NSArray *dataArray = @[NSLocalizedString(@"Page Size", nil), NSLocalizedString(@"Page Direction", 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 a blank 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.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- self.tableView.rowHeight = UITableViewAutomaticDimension;
- self.tableView.estimatedRowHeight = 44.0;
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
- [self.view addSubview:self.tableView];
-
- self.isSelect = NO;
- self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
-
- self.pageModel = [[CBlankPageModel alloc] init];
- self.pageModel.pageIndex = 0;
- self.pageModel.size = CGSizeMake(210, 297);
- self.pageModel.rotation = 0;
-
- self.pageLoactionBtns = [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);
-
- 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 - Action
- - (void)buttonItemClicked_save:(id)sender {
- [self dismissViewControllerAnimated:YES completion:^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(pageInsertViewControllerSave:pageModel:)]) {
- [self.delegate pageInsertViewControllerSave:self pageModel:self.pageModel];
- }
- }];
- }
- - (void)buttonItemClicked_cancel:(id)sender {
- [self dismissViewControllerAnimated:YES completion:^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(pageInsertViewControllerCancel:)]) {
- [self.delegate pageInsertViewControllerCancel:self];
- }
- }];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.dataArray.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 60.0;
- }
- - (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]];
- break;
- case 1:
- {
- if (self.isSelect) {
- [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- } else {
- [cell setCellStyle:CInsertBlankPageCellDirection label:self.dataArray[indexPath.row]];
- }
- }
- break;
- case 2:
- {
- if (self.isSelect) {
- [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- } else {
- [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
- }
- }
- break;
- case 3:
- {
- if (self.isSelect) {
- [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
- } else {
- [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
- if (!self.currentPageIndex) {
- self.preCell = cell;
- cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
- cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- }
-
- if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
- [self.pageLoactionBtns addObject:cell.locationSelectBtn];
- }
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- }
- break;
- case 4:
- {
- [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 5:
- {
- [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 6:
- {
- [cell setCellStyle:CInsertBlankPageCellLocationTextFiled label:self.dataArray[indexPath.row]];
- if (self.currentPageIndex) {
- cell.locationTextField.text = [NSString stringWithFormat:@"%lu", self.currentPageIndex];
- self.pageModel.pageIndex = self.currentPageIndex;
- }
- self.locationTextField = cell.locationTextField;
- cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
- }
- break;
- case 7:
- {
- [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 - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
- CInsertBlankPageCell *cell = [tableView cellForRowAtIndexPath:path];
- switch (indexPath.row) {
- case 1:
- {
- if (self.isSelect) {
- self.pageModel.size = CGSizeMake(297, 420);
- cell.sizeLabel.text = @"A3 (297 X 420)";
- }
- }
- break;
- case 2:
- {
- if (self.isSelect) {
- self.pageModel.size = CGSizeMake(210, 297);
- cell.sizeLabel.text = @"A4 (210 X 297)";
- }
- }
- break;
- case 3:
- {
- if (self.isSelect) {
- self.pageModel.size = CGSizeMake(148, 210);
- cell.sizeLabel.text = @"A5 (148 X 210)";
- }
- }
- break;
-
- default:
- break;
- }
- }
- #pragma mark - CInsertBlankPageCellDelegate
- - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell isSelect:(BOOL)isSelect {
- NSArray *szieArray = @[@"A3 (297 X 420mm)", @"A4 (210 X 297mm)", @"A5 (148 X 210mm)"];
- NSIndexPath *indexPath = [self.tableView indexPathForCell:insertBlankPageCell];
- self.isSelect = isSelect;
-
- if (isSelect) {
- NSInteger t = indexPath.row;
- NSMutableArray *array = [NSMutableArray array];
- NSMutableArray *data = [NSMutableArray arrayWithArray:self.dataArray];
- for (NSString *str in szieArray) {
- t++;
- NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
- [array addObject:path];
-
- [data insertObject:str atIndex:t];
- }
- self.dataArray = (NSArray *)data;
- [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
- } else {
- NSInteger t = indexPath.row;
- NSMutableArray *array = [NSMutableArray array];
- NSMutableArray *data = [NSMutableArray arrayWithArray:self.dataArray];
- for (int i = 0; i < szieArray.count; i++) {
- t++;
- NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
- [array addObject:path];
- [data removeObjectAtIndex:t];
- }
- self.dataArray = (NSArray *)data;
- [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
- }
- }
- - (void)insertBlankPageCellLocation:(CInsertBlankPageCell *)insertBlankPageCell button:(nonnull UIButton *)buttom {
- if (self.preCell) {
- self.preCell.locationSelectBtn.selected = !self.preCell.locationSelectBtn.selected;
- self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
- if (self.preCell.locationSelectBtn != buttom) {
- buttom.selected = !buttom.selected;
- insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
- self.preCell = insertBlankPageCell;
- } else {
- self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
- self.preCell = nil;
- }
- } else {
- self.preCell = 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.text = @"";
- break;
- case 1:
- self.pageModel.pageIndex = -2;
- self.locationTextField.text = @"";
- break;
- case 2:
- self.pageModel.pageIndex = self.pageModel.pageIndex - 1;
- break;
- case 3:
- self.pageModel.pageIndex = self.pageModel.pageIndex + 1;
- break;
-
- default:
- break;
- }
- }
- - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell pageIndex:(NSInteger)pageIndex {
- self.pageModel.pageIndex = pageIndex;
- }
- - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell rotate:(NSInteger)rotate {
- self.pageModel.rotation = rotate;
- }
- #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
|