CPDFBatesAddViewController.m 14 KB

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