// // CPDFBackgroundSettingViewController.m // PDFViewer // // Created by kdanmobile_2 on 2023/1/2. // #import "PDFBackgroundSettingViewController.h" #import "PDFBackgroundView.h" #import "PDFBackgroundPreview.h" #import "PDFBackgroundSettingView.h" #import "PDFDrawBackgroundView.h" #import "PDFBackgroundDrawImageView.h" @interface PDFBackgroundSettingViewController () @property (nonatomic,strong) PDFBackgroundModel *dataModel; @property (nonatomic,assign) CGSize imageSize; @property (nonatomic,strong) UIImage *image; @property (nonatomic,strong) CPDFDocument *document; @property (retain, nonatomic) IBOutlet UIButton *colorBtn1; @property (retain, nonatomic) IBOutlet UIButton *colorBtn2; @property (retain, nonatomic) IBOutlet UIButton *colorBtn3; @property (retain, nonatomic) IBOutlet UIButton *colorBtn4; @property (retain, nonatomic) IBOutlet UIButton *colorBtn5; @property (retain, nonatomic) IBOutlet UIButton *colorBtn6; @property (retain, nonatomic) IBOutlet UIButton *colorBtn7; @property (retain, nonatomic) IBOutlet UIButton *colorBtn8; @property (retain, nonatomic) IBOutlet UIButton *selectImageBtn; @property (retain, nonatomic) IBOutlet UISlider *rotationSlider; @property (retain, nonatomic) IBOutlet UISlider *opacitySlider; @property (retain, nonatomic) IBOutlet UISlider *scaleSlider; @property (retain, nonatomic) IBOutlet UIButton *pageBtn; @property (retain, nonatomic) IBOutlet UITextField *horizontalField; @property (retain, nonatomic) IBOutlet UITextField *verticalField; @property (retain, nonatomic) IBOutlet UIButton *cancelBtn; @property (retain, nonatomic) IBOutlet UIButton *doneBtn; @property (retain, nonatomic) IBOutlet UIImageView *documentImageView; @property (retain, nonatomic) IBOutlet PDFDrawBackgroundView *drawBackgroundView; @property (retain, nonatomic) IBOutlet UIView *backgroudSettingView; @property (retain, nonatomic) IBOutlet PDFBackgroundDrawImageView *drawImageView; @property (retain, nonatomic) IBOutlet UILabel *rangeLabel; @end @implementation PDFBackgroundSettingViewController #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)]; UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Setting", nil) style:UIBarButtonItemStylePlain target:self action:@selector(editClick)]; UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Delete", nil) style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)]; self.navigationItem.rightBarButtonItems = @[doneBtn, editBtn, deleteBtn]; self.view.backgroundColor = [UIColor whiteColor]; [self initDataMoel]; [self createView]; } #pragma mark - Initializers - (instancetype)initWithDocument:(CPDFDocument *)document { self = [super init]; if (self) { _document = document; CPDFPage *pdfPage = [document pageAtIndex:0]; CGSize imageSize = [document pageSizeAtIndex:0]; _image = [pdfPage thumbnailOfSize:imageSize]; _imageSize = imageSize; } return self; } - (void)createView { [_colorBtn1 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn2 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn3 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn4 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn5 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn6 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn7 setTitle:@" " forState:UIControlStateNormal]; [_colorBtn8 setTitle:@" " forState:UIControlStateNormal]; _documentImageView.image = self.image; _drawBackgroundView.dataModel = self.dataModel; [_drawImageView setImage:_image]; } - (void)initDataMoel { _dataModel = [[PDFBackgroundModel alloc] init]; _dataModel.backgroundColor = [UIColor whiteColor]; _dataModel.backgroudScale = 1.0f; _dataModel.backgroundOpacity = 1.0f; _dataModel.backgroundRotation = 0.0f; _dataModel.verticalSpacing = 0.0f; _dataModel.horizontalSpacing = 0.0f; _dataModel.pageString = @"0"; } #pragma mark - Actions - (void)onClickedDoneBtn { [self.delegate PDFViewPerformChangeBackground:self.dataModel]; [self.navigationController popViewControllerAnimated:YES]; } - (void)editClick { self.selectImageBtn.enabled = NO; self.navigationItem.rightBarButtonItems.firstObject.enabled = NO; self.navigationItem.rightBarButtonItems.lastObject.enabled = NO; _backgroudSettingView.hidden = NO; _verticalField.delegate = self; _horizontalField.delegate = self; } - (void)deleteClick { [self.delegate PDFViewPerformDeleteBackground]; [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - UI Functions - (IBAction)onColorBtnClicked:(UIButton *)sender { if (_dataModel.image) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Select Color Warning", nil) message:NSLocalizedString(@"Sorry,we can't choose color", nil) preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } _dataModel.backgroundColor = sender.backgroundColor; _drawBackgroundView.dataModel = self.dataModel; [_drawBackgroundView setNeedsDisplay]; } - (IBAction)onSelectBtnClicked:(UIButton *)sender { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; imagePicker.allowsEditing = YES; [self presentViewController:imagePicker animated:YES completion:nil]; } - (IBAction)onSelectPageRange:(UIButton *)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"All Pages", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.rangeLabel.text = NSLocalizedString(@"Page Range: ALL", nil); self.dataModel.pageString = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1]; }]; UIAlertAction *customRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"Custom", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self createCustomRangeAlert]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:defaultRange]; [alertController addAction:customRange]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; } - (void)createCustomRangeAlert { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Custom Page Range", nil) message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = NSLocalizedString(@"such as:1,3-5,10", nil);; }]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Done", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.dataModel.pageString = alertController.textFields.firstObject.text; self.rangeLabel.text = NSLocalizedString(@"Page Range:Custom", nil); }]]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } - (IBAction)doneClick:(UIButton *)sender { [self btnEnable]; _drawBackgroundView.dataModel = self.dataModel; [_drawBackgroundView setNeedsDisplay]; _backgroudSettingView.hidden = YES; } - (IBAction)cancelClick:(UIButton *)sender { [self btnEnable]; _backgroudSettingView.hidden = YES; } - (IBAction)onOpacityChanged:(UISlider *)sender { _dataModel.backgroundOpacity = 1 - sender.value; } - (IBAction)onScaleChanged:(UISlider *)sender { _dataModel.backgroudScale = sender.value; } - (IBAction)onRotationChanged:(UISlider *)sender { _dataModel.backgroundRotation = sender.value * 180 / M_PI; } - (IBAction)horizontalChange:(UITextField *)sender { _dataModel.horizontalSpacing = [sender.text floatValue]; } - (IBAction)verticalChage:(UITextField *)sender { _dataModel.verticalSpacing = [sender.text floatValue]; } - (void)btnEnable { self.selectImageBtn.enabled = YES; self.navigationItem.rightBarButtonItems.firstObject.enabled = YES; self.navigationItem.rightBarButtonItems.lastObject.enabled = YES; } #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = info[UIImagePickerControllerOriginalImage]; CGSize size; size.width = self.documentImageView.frame.size.width; size.height = self.documentImageView.frame.size.height; _dataModel.image = [self imageWithImageSimple:image scaledToSize:_imageSize]; // [_backgroundPreView.preImageView sizeToFit]; [picker dismissViewControllerAnimated:YES completion:nil]; [_drawBackgroundView setNeedsDisplay]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (_horizontalField == textField || _verticalField == textField) { return [self validateValue:string]; } return YES; } - (BOOL)validateValue:(NSString *)number { BOOL res = YES; NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; NSInteger i = 0; while (i < number.length) { NSString *str = [number substringWithRange:NSMakeRange(i, 1)]; NSRange range = [str rangeOfCharacterFromSet:numberSet]; if (range.length == 0) { res = NO; break; } i++; } return res; } @end