CPDFHeaderFooterAddController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // HeaderFooterAlertViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2022/11/16.
  6. //
  7. #import "CPDFHeaderFooterAddController.h"
  8. @interface CPDFHeaderFooterAddController ()
  9. @property (nonatomic,strong) CPDFHeaderFooterAddView *headerFooterview;
  10. @property (nonatomic,strong) CPDFHeaderFooterTextTableView *pageFormat;
  11. @property (nonatomic,strong) NSMutableArray *dataArray;
  12. @end
  13. @implementation CPDFHeaderFooterAddController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view.
  17. // Setting navigation
  18. [self initNavigation];
  19. // Initialize show label
  20. [self initShowLabel];
  21. // Initialize model
  22. [self initModel];
  23. // Add page image
  24. [self initIamge];
  25. // Add view
  26. [self initBttonmView];
  27. // UI event
  28. [self initBasicEvent];
  29. [self initExtendEvent];
  30. }
  31. #pragma mark - Initializers
  32. - (void)initNavigation {
  33. self.navigationItem.title = @"Add Page NUmber";
  34. self.navigationController.toolbarHidden = YES;
  35. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClick:)];
  36. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelClick:)];
  37. }
  38. - (id)initWithIamge:(UIImage *)image WithSize:(CGSize)size {
  39. self = [super init];
  40. if (self) {
  41. _image = image;
  42. _size = size;
  43. }
  44. return self;
  45. }
  46. - (void)initModel {
  47. _modelData = [[CPDFHeaderFooterModel alloc] init];
  48. _modelData.fontSize = 18.0;
  49. _modelData.fontSelcet = 0;
  50. _modelData.fontPosition = 1;
  51. _modelData.pageStart = @"1";
  52. }
  53. - (void)initShowLabel {
  54. _showLabel = [[UILabel alloc] init];
  55. [_showLabel setText:@"1"];
  56. _showLabel.font = [UIFont systemFontOfSize:6];
  57. }
  58. - (void)initIamge {
  59. _imageView = [[UIImageView alloc] init];
  60. _imageView.image = self.image;
  61. CALayer *layer = [_imageView layer];
  62. layer.borderColor = [[UIColor blackColor] CGColor];
  63. layer.borderWidth = 1.0f;
  64. [self.view addSubview:_imageView];
  65. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.top.equalTo(self.view.mas_top).offset(125);
  67. make.left.equalTo(self.view.mas_left).offset((393 / 2) - (self.size.width / 6));
  68. make.width.mas_equalTo(self.size.width / 3);
  69. make.height.mas_equalTo(self.size.height / 3);
  70. }];
  71. }
  72. - (void)initBttonmView {
  73. _headerFooterview = [[CPDFHeaderFooterAddView alloc] init];
  74. [_headerFooterview setText];
  75. [self.view addSubview:_headerFooterview];
  76. [self.headerFooterview mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.top.equalTo(self.imageView.mas_bottom).offset(60);
  78. make.left.equalTo(self.view.mas_left);
  79. make.width.mas_equalTo(self.view.frame.size.width);
  80. make.height.mas_equalTo(self.view.frame.size.height);
  81. }];
  82. }
  83. - (void)initBasicEvent {
  84. [_headerFooterview.localSegment addTarget:self action:@selector(changeLocation:) forControlEvents:UIControlEventValueChanged];
  85. [_headerFooterview.aligbmentSegment addTarget:self action:@selector(changeAligbment:) forControlEvents:UIControlEventValueChanged];
  86. [_headerFooterview.colorSlider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
  87. }
  88. - (void)initExtendEvent {
  89. [_headerFooterview.fontSizeText addTarget:self action:@selector(changeFontSize:) forControlEvents:UIControlEventEditingChanged];
  90. [_headerFooterview.pageIndexNumberText addTarget:self action:@selector(changePageStart:) forControlEvents:UIControlEventEditingChanged];
  91. _headerFooterview.pageNumberText.delegate = self;
  92. }
  93. #pragma mark - Accessors
  94. - (NSArray *)dataArray {
  95. if (!_dataArray) {
  96. _dataArray = [NSMutableArray array];
  97. NSArray *pageFormatArray = [[NSArray alloc] initWithObjects:@"1",@"page 1",@"1/n",@"1 of n",@"page 1 of n",nil];
  98. for (int i = 0; i < pageFormatArray.count; i++) {
  99. CPDFPageFomatModel *pageFormatMode = [[CPDFPageFomatModel alloc] init];
  100. pageFormatMode.pageFormat = pageFormatArray[i];
  101. [_dataArray addObject:pageFormatMode];
  102. }
  103. }
  104. return _dataArray;
  105. }
  106. #pragma mark - UITextFieldDelegate
  107. // TableView initialize
  108. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  109. _pageFormat = [[CPDFHeaderFooterTextTableView alloc] initWithTitleName:@"Format Selection"];
  110. _pageFormat.tableView.delegate = self;
  111. _pageFormat.tableView.dataSource = self;
  112. [self.headerFooterview addSubview:self.pageFormat];
  113. [self.view bringSubviewToFront:self.pageFormat];
  114. [_pageFormat mas_makeConstraints:^(MASConstraintMaker *make) {
  115. make.top.equalTo(self.headerFooterview.mas_top).offset(50);
  116. make.left.equalTo(self.headerFooterview.mas_left).offset(50);
  117. make.width.mas_equalTo(293);
  118. make.height.mas_equalTo(300);
  119. }];
  120. // Tablevire animation
  121. [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  122. NSLog(@"animations");
  123. self.pageFormat.center = CGPointMake(self.pageFormat.center.x + 150 + [UIScreen mainScreen].bounds.size.width / 2, self.pageFormat.center.y);
  124. } completion:^(BOOL finished) {
  125. NSLog(@"completion");
  126. }];
  127. [self.pageFormat.okBtn addTarget:self action:@selector(pageFormatOk:) forControlEvents:UIControlEventTouchUpInside];
  128. }
  129. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  130. return NO;
  131. }
  132. #pragma mark - UITableViewDataSource
  133. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  134. return self.dataArray.count;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  137. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pageFormat"];
  138. if (cell == nil) {
  139. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pageFormat"];
  140. }
  141. cell.textLabel.text = [self.dataArray[indexPath.row] pageFormat];
  142. return cell;
  143. }
  144. #pragma mark - UITableViewDelegate
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. self.headerFooterview.pageNumberText.text = [self.dataArray[indexPath.row] pageFormat];
  147. self.modelData.fontSelcet = indexPath.row;
  148. self.showLabel.text = [self.dataArray[indexPath.row] pageFormat];
  149. }
  150. #pragma mark - Actions
  151. // Candel button enable
  152. - (void)cancelClick:(UIBarButtonItem *)btn {
  153. [self.navigationController popViewControllerAnimated:YES];
  154. }
  155. // Done button enable
  156. - (void)doneClick:(UIBarButtonItem *)btn {
  157. NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:self.modelData,@"model", nil];
  158. NSNotification *notification = [NSNotification notificationWithName:@"getAddHeaderFooterModel" object:nil userInfo:dict];
  159. [[NSNotificationCenter defaultCenter] postNotification:notification];
  160. [self.navigationController popViewControllerAnimated:YES];
  161. }
  162. - (void)pageFormatOk:(UIButton *)okBtn {
  163. [self.pageFormat removeFromSuperview];
  164. }
  165. #pragma mark - UI Actions
  166. // Get page start for headerfooter's Model
  167. - (void)changePageStart:(UITextField *)text {
  168. int start = [text.text intValue];
  169. self.modelData.pageStart = [NSString stringWithFormat:@"%d",start];
  170. }
  171. // Get headerfoote's font size for headerfooter's Model
  172. - (void)changeFontSize:(UITextField *)text {
  173. float size = [text.text floatValue];
  174. _showLabel.font = [UIFont systemFontOfSize:size / 3];
  175. _modelData.fontSize = size;
  176. }
  177. // Slider select color
  178. - (void)sliderChange:(UISlider *)slider {
  179. switch ((int)slider.value / 10) {
  180. case 0:
  181. self.showLabel.textColor = [UIColor redColor];
  182. self.modelData.fontColor = [UIColor redColor];
  183. break;
  184. case 1:
  185. self.showLabel.textColor = [UIColor orangeColor];
  186. self.modelData.fontColor = [UIColor orangeColor];
  187. break;
  188. case 2:
  189. self.showLabel.textColor = [UIColor colorWithRed:239.0 / 255 green:140.0 / 255 blue:133.0 / 255 alpha:1];
  190. self.modelData.fontColor = [UIColor colorWithRed:239.0 / 255 green:140.0 / 255 blue:133.0 / 255 alpha:1];
  191. break;
  192. case 3:
  193. self.showLabel.textColor = [UIColor yellowColor];
  194. self.modelData.fontColor = [UIColor yellowColor];;
  195. break;
  196. case 4:
  197. self.showLabel.textColor = [UIColor greenColor];
  198. self.modelData.fontColor = [UIColor greenColor];
  199. break;
  200. case 5:
  201. self.showLabel.textColor = [UIColor blueColor];
  202. self.modelData.fontColor = [UIColor blueColor];
  203. break;
  204. case 6:
  205. self.showLabel.textColor = [UIColor purpleColor];
  206. self.modelData.fontColor = [UIColor purpleColor];
  207. break;
  208. case 7:
  209. self.showLabel.textColor = [UIColor colorWithRed:235.0 / 255 green:61.0 / 255 blue:133.0 / 255 alpha:1];
  210. self.modelData.fontColor = [UIColor colorWithRed:235.0 / 255 green:61.0 / 255 blue:133.0 / 255 alpha:1];
  211. break;
  212. case 8:
  213. self.showLabel.textColor = [UIColor blackColor];
  214. self.modelData.fontColor = [UIColor blackColor];
  215. break;
  216. case 9:
  217. self.showLabel.textColor = [UIColor whiteColor];
  218. self.modelData.fontColor = [UIColor whiteColor];
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. // Select headerfooter
  225. - (void)changeLocation:(UISegmentedControl *)sender {
  226. if (sender.selectedSegmentIndex == 0) {
  227. _position.location = kHeader;
  228. } else {
  229. _position.location = kFooter;
  230. }
  231. [self showPages];
  232. }
  233. // Select aligment
  234. - (void)changeAligbment:(UISegmentedControl *)sender {
  235. if (sender.selectedSegmentIndex == 0) {
  236. _position.aligment = kLeft;
  237. } else if (sender.selectedSegmentIndex == 1) {
  238. _position.aligment = kCenter;
  239. } else {
  240. _position.aligment = kRinght;
  241. }
  242. [self showPages];
  243. }
  244. // Show headerfooter and aligment
  245. - (void)showPages {
  246. if (_showLabel == nil) {
  247. _showLabel = [[UILabel alloc] init];
  248. }
  249. [self.imageView addSubview:self.showLabel];
  250. switch (self.position.location) {
  251. case kHeader:
  252. if (self.position.aligment == kLeft) {
  253. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  254. make.top.equalTo(_imageView.mas_top).offset(0.5);
  255. make.left.equalTo(self.imageView.mas_left).offset(0.5);
  256. make.height.mas_equalTo(10);
  257. make.width.mas_equalTo(20);
  258. }];
  259. self.modelData.fontPosition = 0;
  260. } else if (self.position.aligment == kCenter) {
  261. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  262. make.top.equalTo(_imageView.mas_top).offset(0.5);
  263. make.left.equalTo(self.imageView.mas_left).offset(self.size.width / 6);
  264. make.height.mas_equalTo(10);
  265. make.width.mas_equalTo(20);
  266. }];
  267. self.modelData.fontPosition = 1;
  268. } else {
  269. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  270. make.top.equalTo(self.imageView.mas_top).offset(0.5);
  271. make.left.equalTo(self.imageView.mas_left).offset(self.size.width / 3 - 12);
  272. make.height.mas_equalTo(10);
  273. make.width.mas_equalTo(20);
  274. }];
  275. self.modelData.fontPosition = 2;
  276. }
  277. break;
  278. case kFooter:
  279. if (self.position.aligment == kLeft) {
  280. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  281. make.top.equalTo(self.imageView.mas_top).offset(self.size.height / 3 - 12);
  282. make.left.equalTo(self.imageView.mas_left).offset(0.5);
  283. make.height.mas_equalTo(10);
  284. make.width.mas_equalTo(20);
  285. }];
  286. self.modelData.fontPosition = 3;
  287. } else if (self.position.aligment == kCenter) {
  288. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  289. make.top.equalTo(self.imageView.mas_top).offset(self.size.height / 3 - 12);
  290. make.left.equalTo(self.imageView.mas_left).offset(self.size.width/6);
  291. make.height.mas_equalTo(10);
  292. make.width.mas_equalTo(20);
  293. }];
  294. self.modelData.fontPosition = 4;
  295. } else {
  296. [_showLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  297. make.top.equalTo(self.imageView.mas_top).offset(self.size.height / 3 - 12);
  298. make.left.equalTo(self.imageView.mas_left).offset(self.size.width / 3 - 12);
  299. make.height.mas_equalTo(10);
  300. make.width.mas_equalTo(20);
  301. }];
  302. self.modelData.fontPosition = 5;
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. #pragma mark - Orientation
  310. // When orientation
  311. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  312. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  313. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  314. make.top.equalTo(self.view.mas_top).offset((393 / 2) - (self.size.height / 6));
  315. make.left.equalTo(self.view.mas_left).offset((393 / 2) - (self.size.width/6));
  316. make.width.mas_equalTo(self.size.width / 3);
  317. make.height.mas_equalTo(self.size.height / 3);
  318. }];
  319. [self.headerFooterview mas_remakeConstraints:^(MASConstraintMaker *make) {
  320. make.top.equalTo(self.view.mas_top).offset(15);
  321. make.left.equalTo(self.view.mas_left).offset(self.view.frame.size.width / 2 + 200);
  322. make.width.mas_equalTo(393);
  323. make.height.mas_equalTo(self.view.frame.size.height - 20);
  324. }];
  325. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  326. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  327. make.top.equalTo(self.view.mas_top).offset(125);
  328. make.left.equalTo(self.view.mas_left).offset((393 / 2) - (self.size.width / 6));
  329. make.width.mas_equalTo(self.size.width / 3);
  330. make.height.mas_equalTo(self.size.height / 3);
  331. }];
  332. [self.headerFooterview mas_remakeConstraints:^(MASConstraintMaker *make) {
  333. make.top.equalTo(self.imageView.mas_bottom).offset(60);
  334. make.left.equalTo(self.view.mas_left);
  335. make.width.mas_equalTo(self.view.frame.size.width);
  336. make.height.mas_equalTo(self.view.frame.size.height);
  337. }];
  338. }
  339. }
  340. @end