PDFBackgroundSettingViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // CPDFBackgroundSettingViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2023/1/2.
  6. //
  7. #import "PDFBackgroundSettingViewController.h"
  8. #import "PDFBackgroundView.h"
  9. #import "PDFBackgroundPreview.h"
  10. #import "PDFBackgroundSettingView.h"
  11. #import "PDFDrawBackgroundView.h"
  12. #import "PDFBackgroundDrawImageView.h"
  13. @interface PDFBackgroundSettingViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
  14. @property (nonatomic,strong) PDFBackgroundModel *dataModel;
  15. @property (nonatomic,assign) CGSize imageSize;
  16. @property (nonatomic,strong) UIImage *image;
  17. @property (nonatomic,strong) CPDFDocument *document;
  18. @property (retain, nonatomic) IBOutlet UIButton *colorBtn1;
  19. @property (retain, nonatomic) IBOutlet UIButton *colorBtn2;
  20. @property (retain, nonatomic) IBOutlet UIButton *colorBtn3;
  21. @property (retain, nonatomic) IBOutlet UIButton *colorBtn4;
  22. @property (retain, nonatomic) IBOutlet UIButton *colorBtn5;
  23. @property (retain, nonatomic) IBOutlet UIButton *colorBtn6;
  24. @property (retain, nonatomic) IBOutlet UIButton *colorBtn7;
  25. @property (retain, nonatomic) IBOutlet UIButton *colorBtn8;
  26. @property (retain, nonatomic) IBOutlet UIButton *selectImageBtn;
  27. @property (retain, nonatomic) IBOutlet UISlider *rotationSlider;
  28. @property (retain, nonatomic) IBOutlet UISlider *opacitySlider;
  29. @property (retain, nonatomic) IBOutlet UISlider *scaleSlider;
  30. @property (retain, nonatomic) IBOutlet UIButton *pageBtn;
  31. @property (retain, nonatomic) IBOutlet UITextField *horizontalField;
  32. @property (retain, nonatomic) IBOutlet UITextField *verticalField;
  33. @property (retain, nonatomic) IBOutlet UIButton *cancelBtn;
  34. @property (retain, nonatomic) IBOutlet UIButton *doneBtn;
  35. @property (retain, nonatomic) IBOutlet UIImageView *documentImageView;
  36. @property (retain, nonatomic) IBOutlet PDFDrawBackgroundView *drawBackgroundView;
  37. @property (retain, nonatomic) IBOutlet UIView *backgroudSettingView;
  38. @property (retain, nonatomic) IBOutlet PDFBackgroundDrawImageView *drawImageView;
  39. @property (retain, nonatomic) IBOutlet UILabel *rangeLabel;
  40. @end
  41. @implementation PDFBackgroundSettingViewController
  42. #pragma mark - UIViewController Methods
  43. - (void)viewDidLoad {
  44. [super viewDidLoad];
  45. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
  46. UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Setting", nil) style:UIBarButtonItemStylePlain target:self action:@selector(editClick)];
  47. UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Delete", nil) style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
  48. self.navigationItem.rightBarButtonItems = @[doneBtn, editBtn, deleteBtn];
  49. self.view.backgroundColor = [UIColor whiteColor];
  50. [self initDataMoel];
  51. [self createView];
  52. }
  53. #pragma mark - Initializers
  54. - (instancetype)initWithDocument:(CPDFDocument *)document {
  55. self = [super init];
  56. if (self) {
  57. _document = document;
  58. CPDFPage *pdfPage = [document pageAtIndex:0];
  59. CGSize imageSize = [document pageSizeAtIndex:0];
  60. _image = [pdfPage thumbnailOfSize:imageSize];
  61. _imageSize = imageSize;
  62. }
  63. return self;
  64. }
  65. - (void)createView {
  66. [_colorBtn1 setTitle:@" " forState:UIControlStateNormal];
  67. [_colorBtn2 setTitle:@" " forState:UIControlStateNormal];
  68. [_colorBtn3 setTitle:@" " forState:UIControlStateNormal];
  69. [_colorBtn4 setTitle:@" " forState:UIControlStateNormal];
  70. [_colorBtn5 setTitle:@" " forState:UIControlStateNormal];
  71. [_colorBtn6 setTitle:@" " forState:UIControlStateNormal];
  72. [_colorBtn7 setTitle:@" " forState:UIControlStateNormal];
  73. [_colorBtn8 setTitle:@" " forState:UIControlStateNormal];
  74. _documentImageView.image = self.image;
  75. _drawBackgroundView.dataModel = self.dataModel;
  76. [_drawImageView setImage:_image];
  77. }
  78. - (void)initDataMoel {
  79. _dataModel = [[PDFBackgroundModel alloc] init];
  80. _dataModel.backgroundColor = [UIColor whiteColor];
  81. _dataModel.backgroudScale = 1.0f;
  82. _dataModel.backgroundOpacity = 1.0f;
  83. _dataModel.backgroundRotation = 0.0f;
  84. _dataModel.verticalSpacing = 0.0f;
  85. _dataModel.horizontalSpacing = 0.0f;
  86. _dataModel.pageString = @"0";
  87. }
  88. #pragma mark - Actions
  89. - (void)onClickedDoneBtn {
  90. [self.delegate PDFViewPerformChangeBackground:self.dataModel];
  91. [self.navigationController popViewControllerAnimated:YES];
  92. }
  93. - (void)editClick {
  94. self.selectImageBtn.enabled = NO;
  95. self.navigationItem.rightBarButtonItems.firstObject.enabled = NO;
  96. self.navigationItem.rightBarButtonItems.lastObject.enabled = NO;
  97. _backgroudSettingView.hidden = NO;
  98. _verticalField.delegate = self;
  99. _horizontalField.delegate = self;
  100. }
  101. - (void)deleteClick {
  102. [self.delegate PDFViewPerformDeleteBackground];
  103. [self.navigationController popViewControllerAnimated:YES];
  104. }
  105. #pragma mark - UI Functions
  106. - (IBAction)onColorBtnClicked:(UIButton *)sender {
  107. if (_dataModel.image) {
  108. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Select Color Warning", nil) message:NSLocalizedString(@"Sorry,we can't choose color", nil) preferredStyle:UIAlertControllerStyleAlert];
  109. [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  110. }]];
  111. [self presentViewController:alertController animated:YES completion:nil];
  112. }
  113. _dataModel.backgroundColor = sender.backgroundColor;
  114. _drawBackgroundView.dataModel = self.dataModel;
  115. [_drawBackgroundView setNeedsDisplay];
  116. }
  117. - (IBAction)onSelectBtnClicked:(UIButton *)sender {
  118. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  119. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  120. imagePicker.delegate = self;
  121. imagePicker.allowsEditing = YES;
  122. [self presentViewController:imagePicker animated:YES completion:nil];
  123. }
  124. - (IBAction)onSelectPageRange:(UIButton *)sender {
  125. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  126. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"All Pages", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  127. self.rangeLabel.text = NSLocalizedString(@"Page Range: ALL", nil);
  128. self.dataModel.pageString = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
  129. }];
  130. UIAlertAction *customRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"Custom", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  131. [self createCustomRangeAlert];
  132. }];
  133. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  134. }];
  135. [alertController addAction:defaultRange];
  136. [alertController addAction:customRange];
  137. [alertController addAction:cancelAction];
  138. [self presentViewController:alertController animated:YES completion:nil];
  139. }
  140. - (void)createCustomRangeAlert {
  141. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Custom Page Range", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  142. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  143. textField.placeholder = NSLocalizedString(@"such as:1,3-5,10", nil);;
  144. }];
  145. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Done", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  146. self.dataModel.pageString = alertController.textFields.firstObject.text;
  147. self.rangeLabel.text = NSLocalizedString(@"Page Range:Custom", nil);
  148. }]];
  149. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  150. }]];
  151. [self presentViewController:alertController animated:YES completion:nil];
  152. }
  153. - (IBAction)doneClick:(UIButton *)sender {
  154. [self btnEnable];
  155. _drawBackgroundView.dataModel = self.dataModel;
  156. [_drawBackgroundView setNeedsDisplay];
  157. _backgroudSettingView.hidden = YES;
  158. }
  159. - (IBAction)cancelClick:(UIButton *)sender {
  160. [self btnEnable];
  161. _backgroudSettingView.hidden = YES;
  162. }
  163. - (IBAction)onOpacityChanged:(UISlider *)sender {
  164. _dataModel.backgroundOpacity = 1 - sender.value;
  165. }
  166. - (IBAction)onScaleChanged:(UISlider *)sender {
  167. _dataModel.backgroudScale = sender.value;
  168. }
  169. - (IBAction)onRotationChanged:(UISlider *)sender {
  170. _dataModel.backgroundRotation = sender.value * 180 / M_PI;
  171. }
  172. - (IBAction)horizontalChange:(UITextField *)sender {
  173. _dataModel.horizontalSpacing = [sender.text floatValue];
  174. }
  175. - (IBAction)verticalChage:(UITextField *)sender {
  176. _dataModel.verticalSpacing = [sender.text floatValue];
  177. }
  178. - (void)btnEnable {
  179. self.selectImageBtn.enabled = YES;
  180. self.navigationItem.rightBarButtonItems.firstObject.enabled = YES;
  181. self.navigationItem.rightBarButtonItems.lastObject.enabled = YES;
  182. }
  183. #pragma mark - UIImagePickerControllerDelegate
  184. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  185. UIImage *image = info[UIImagePickerControllerOriginalImage];
  186. CGSize size;
  187. size.width = self.documentImageView.frame.size.width;
  188. size.height = self.documentImageView.frame.size.height;
  189. _dataModel.image = [self imageWithImageSimple:image scaledToSize:_imageSize];
  190. // [_backgroundPreView.preImageView sizeToFit];
  191. [picker dismissViewControllerAnimated:YES completion:nil];
  192. [_drawBackgroundView setNeedsDisplay];
  193. }
  194. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  195. [picker dismissViewControllerAnimated:YES completion:nil];
  196. }
  197. - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
  198. {
  199. UIGraphicsBeginImageContext(newSize);
  200. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  201. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  202. UIGraphicsEndImageContext();
  203. return newImage;
  204. }
  205. #pragma mark - UITextFieldDelegate
  206. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  207. [textField resignFirstResponder];
  208. return YES;
  209. }
  210. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  211. if (_horizontalField == textField || _verticalField == textField) {
  212. return [self validateValue:string];
  213. }
  214. return YES;
  215. }
  216. - (BOOL)validateValue:(NSString *)number {
  217. BOOL res = YES;
  218. NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  219. NSInteger i = 0;
  220. while (i < number.length) {
  221. NSString *str = [number substringWithRange:NSMakeRange(i, 1)];
  222. NSRange range = [str rangeOfCharacterFromSet:numberSet];
  223. if (range.length == 0) {
  224. res = NO;
  225. break;
  226. }
  227. i++;
  228. }
  229. return res;
  230. }
  231. @end