CPDFPageInsertViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // CPDFPageInsertViewController.m
  3. // compdfkit-tools
  4. //
  5. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  6. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  7. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  8. // This notice may not be removed from this file.
  9. //
  10. #import "CPDFPageInsertViewController.h"
  11. #import "CPDFColorUtils.h"
  12. #import "CInsertBlankPageCell.h"
  13. #import "CBlankPageModel.h"
  14. @interface CPDFPageInsertViewController () <UITableViewDelegate, UITableViewDataSource, CInsertBlankPageCellDelegate>
  15. @property (nonatomic, strong) UIButton *cancelBtn;
  16. @property (nonatomic, strong) UIButton *saveButton;
  17. @property (nonatomic, strong) UIView *headerView;
  18. @property (nonatomic, strong) UILabel *titleLabel;
  19. @property (nonatomic, strong) UITableView *tableView;
  20. @property (nonatomic, strong) NSArray *dataArray;
  21. @property (nonatomic, assign) BOOL isSelect;
  22. @property (nonatomic, strong) NSMutableArray *pageLoactionBtns;
  23. @property (nonatomic, strong) CBlankPageModel *pageModel;
  24. @property (nonatomic, strong) UITextField *locationTextField;
  25. @property (nonatomic, strong) CInsertBlankPageCell *preCell;
  26. @end
  27. @implementation CPDFPageInsertViewController
  28. #pragma mark - Accessors
  29. - (NSArray *)dataArray {
  30. if (!_dataArray) {
  31. 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)];
  32. _dataArray = dataArray;
  33. }
  34. return _dataArray;
  35. }
  36. #pragma mark - ViewController Methods
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. self.headerView = [[UIView alloc] init];
  41. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  42. self.headerView.layer.borderWidth = 1.0;
  43. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  44. [self.view addSubview:self.headerView];
  45. self.titleLabel = [[UILabel alloc] init];
  46. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  47. self.titleLabel.text = NSLocalizedString(@"Insert a blank page", nil);
  48. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  49. self.titleLabel.font = [UIFont systemFontOfSize:20];
  50. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  51. [self.headerView addSubview:self.titleLabel];
  52. self.saveButton = [[UIButton alloc] init];
  53. self.saveButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  54. [self.saveButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
  55. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  56. [self.saveButton addTarget:self action:@selector(buttonItemClicked_save:) forControlEvents:UIControlEventTouchUpInside];
  57. [self.headerView addSubview:self.saveButton];
  58. self.cancelBtn = [[UIButton alloc] init];
  59. self.cancelBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  60. self.cancelBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  61. [self.cancelBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
  62. [self.cancelBtn setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  63. [self.cancelBtn addTarget:self action:@selector(buttonItemClicked_cancel:) forControlEvents:UIControlEventTouchUpInside];
  64. [self.headerView addSubview:self.cancelBtn];
  65. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
  66. self.tableView.delegate = self;
  67. self.tableView.dataSource = self;
  68. self.tableView.rowHeight = UITableViewAutomaticDimension;
  69. self.tableView.estimatedRowHeight = 44.0;
  70. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  71. self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  72. [self.view addSubview:self.tableView];
  73. self.isSelect = NO;
  74. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  75. self.pageModel = [[CBlankPageModel alloc] init];
  76. self.pageModel.pageIndex = 0;
  77. self.pageModel.size = CGSizeMake(210, 297);
  78. self.pageModel.rotation = 0;
  79. self.pageLoactionBtns = [NSMutableArray array];
  80. [[NSNotificationCenter defaultCenter] addObserver:self
  81. selector:@selector(keyboardwillChangeFrame:)
  82. name:UIKeyboardWillChangeFrameNotification
  83. object:nil];
  84. [[NSNotificationCenter defaultCenter] addObserver:self
  85. selector:@selector(keyboardWillHide:)
  86. name:UIKeyboardWillHideNotification
  87. object:nil];
  88. }
  89. - (void)viewWillLayoutSubviews {
  90. [super viewWillLayoutSubviews];
  91. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
  92. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 200)/2, 0, 200, 50);
  93. if (@available(iOS 11.0, *)) {
  94. self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 40);
  95. self.cancelBtn.frame = CGRectMake( self.view.safeAreaInsets.left+20, 5, 50, 40);
  96. } else {
  97. self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 40);
  98. self.cancelBtn.frame = CGRectMake(20, 5, 50, 40);
  99. }
  100. }
  101. #pragma mark - Action
  102. - (void)buttonItemClicked_save:(id)sender {
  103. [self dismissViewControllerAnimated:YES completion:^{
  104. if (self.delegate && [self.delegate respondsToSelector:@selector(pageInsertViewControllerSave:pageModel:)]) {
  105. [self.delegate pageInsertViewControllerSave:self pageModel:self.pageModel];
  106. }
  107. }];
  108. }
  109. - (void)buttonItemClicked_cancel:(id)sender {
  110. [self dismissViewControllerAnimated:YES completion:^{
  111. if (self.delegate && [self.delegate respondsToSelector:@selector(pageInsertViewControllerCancel:)]) {
  112. [self.delegate pageInsertViewControllerCancel:self];
  113. }
  114. }];
  115. }
  116. #pragma mark - UITableViewDataSource
  117. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  118. return self.dataArray.count;
  119. }
  120. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  121. return 60.0;
  122. }
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  124. CInsertBlankPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pageCell"];
  125. if (cell == nil) {
  126. cell = [[CInsertBlankPageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"pageCell"];
  127. cell.delegate = self;
  128. }
  129. switch (indexPath.row) {
  130. case 0:
  131. [cell setCellStyle:CInsertBlankPageCellSize label:self.dataArray[indexPath.row]];
  132. break;
  133. case 1:
  134. {
  135. if (self.isSelect) {
  136. [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
  137. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  138. } else {
  139. [cell setCellStyle:CInsertBlankPageCellDirection label:self.dataArray[indexPath.row]];
  140. }
  141. }
  142. break;
  143. case 2:
  144. {
  145. if (self.isSelect) {
  146. [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
  147. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  148. } else {
  149. [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
  150. }
  151. }
  152. break;
  153. case 3:
  154. {
  155. if (self.isSelect) {
  156. [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
  157. } else {
  158. [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
  159. if (!self.currentPageIndex) {
  160. self.preCell = cell;
  161. cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
  162. cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
  163. }
  164. if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
  165. [self.pageLoactionBtns addObject:cell.locationSelectBtn];
  166. }
  167. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  168. }
  169. }
  170. break;
  171. case 4:
  172. {
  173. [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
  174. if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
  175. [self.pageLoactionBtns addObject:cell.locationSelectBtn];
  176. }
  177. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  178. }
  179. break;
  180. case 5:
  181. {
  182. [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
  183. if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
  184. [self.pageLoactionBtns addObject:cell.locationSelectBtn];
  185. }
  186. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  187. }
  188. break;
  189. case 6:
  190. {
  191. [cell setCellStyle:CInsertBlankPageCellLocationTextFiled label:self.dataArray[indexPath.row]];
  192. if (self.currentPageIndex) {
  193. cell.locationTextField.text = [NSString stringWithFormat:@"%lu", self.currentPageIndex];
  194. self.pageModel.pageIndex = self.currentPageIndex;
  195. }
  196. self.locationTextField = cell.locationTextField;
  197. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  198. }
  199. break;
  200. case 7:
  201. {
  202. [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
  203. if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
  204. [self.pageLoactionBtns addObject:cell.locationSelectBtn];
  205. }
  206. cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. return cell;
  213. }
  214. #pragma mark - UITableViewDelegate
  215. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  216. NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
  217. CInsertBlankPageCell *cell = [tableView cellForRowAtIndexPath:path];
  218. switch (indexPath.row) {
  219. case 1:
  220. {
  221. if (self.isSelect) {
  222. self.pageModel.size = CGSizeMake(297, 420);
  223. cell.sizeLabel.text = @"A3 (297 X 420)";
  224. }
  225. }
  226. break;
  227. case 2:
  228. {
  229. if (self.isSelect) {
  230. self.pageModel.size = CGSizeMake(210, 297);
  231. cell.sizeLabel.text = @"A4 (210 X 297)";
  232. }
  233. }
  234. break;
  235. case 3:
  236. {
  237. if (self.isSelect) {
  238. self.pageModel.size = CGSizeMake(148, 210);
  239. cell.sizeLabel.text = @"A5 (148 X 210)";
  240. }
  241. }
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. #pragma mark - CInsertBlankPageCellDelegate
  248. - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell isSelect:(BOOL)isSelect {
  249. NSArray *szieArray = @[@"A3 (297 X 420mm)", @"A4 (210 X 297mm)", @"A5 (148 X 210mm)"];
  250. NSIndexPath *indexPath = [self.tableView indexPathForCell:insertBlankPageCell];
  251. self.isSelect = isSelect;
  252. if (isSelect) {
  253. NSInteger t = indexPath.row;
  254. NSMutableArray *array = [NSMutableArray array];
  255. NSMutableArray *data = [NSMutableArray arrayWithArray:self.dataArray];
  256. for (NSString *str in szieArray) {
  257. t++;
  258. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  259. [array addObject:path];
  260. [data insertObject:str atIndex:t];
  261. }
  262. self.dataArray = (NSArray *)data;
  263. [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
  264. } else {
  265. NSInteger t = indexPath.row;
  266. NSMutableArray *array = [NSMutableArray array];
  267. NSMutableArray *data = [NSMutableArray arrayWithArray:self.dataArray];
  268. for (int i = 0; i < szieArray.count; i++) {
  269. t++;
  270. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  271. [array addObject:path];
  272. [data removeObjectAtIndex:t];
  273. }
  274. self.dataArray = (NSArray *)data;
  275. [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
  276. }
  277. }
  278. - (void)insertBlankPageCellLocation:(CInsertBlankPageCell *)insertBlankPageCell button:(nonnull UIButton *)buttom {
  279. if (self.preCell) {
  280. self.preCell.locationSelectBtn.selected = !self.preCell.locationSelectBtn.selected;
  281. self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
  282. if (self.preCell.locationSelectBtn != buttom) {
  283. buttom.selected = !buttom.selected;
  284. insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
  285. self.preCell = insertBlankPageCell;
  286. } else {
  287. self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
  288. self.preCell = nil;
  289. }
  290. } else {
  291. self.preCell = insertBlankPageCell;
  292. buttom.selected = !buttom.selected;
  293. insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
  294. }
  295. NSInteger location = [self.pageLoactionBtns indexOfObject:buttom];
  296. switch (location) {
  297. case 0:
  298. self.pageModel.pageIndex = 0;
  299. self.locationTextField.text = @"";
  300. break;
  301. case 1:
  302. self.pageModel.pageIndex = -2;
  303. self.locationTextField.text = @"";
  304. break;
  305. case 2:
  306. self.pageModel.pageIndex = self.pageModel.pageIndex - 1;
  307. break;
  308. case 3:
  309. self.pageModel.pageIndex = self.pageModel.pageIndex + 1;
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell pageIndex:(NSInteger)pageIndex {
  316. self.pageModel.pageIndex = pageIndex;
  317. }
  318. - (void)insertBlankPageCell:(CInsertBlankPageCell *)insertBlankPageCell rotate:(NSInteger)rotate {
  319. self.pageModel.rotation = rotate;
  320. }
  321. #pragma mark - NSNotification
  322. - (void)keyboardwillChangeFrame:(NSNotification *)notification {
  323. NSDictionary *userInfo = [notification userInfo];
  324. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  325. CGRect frame = value.CGRectValue;
  326. CGRect rect = [self.locationTextField convertRect:self.locationTextField.frame toView:self.view];
  327. if(CGRectGetMaxY(rect) > self.view.frame.size.height - frame.size.height) {
  328. UIEdgeInsets insets = self.tableView.contentInset;
  329. insets.bottom = frame.size.height + self.locationTextField.frame.size.height;
  330. self.tableView.contentInset = insets;
  331. }
  332. }
  333. - (void)keyboardWillHide:(NSNotification *)notification {
  334. UIEdgeInsets insets = self.tableView.contentInset;
  335. insets.bottom = 0;
  336. self.tableView.contentInset = insets;
  337. }
  338. @end