// // CPDFAddViewController.m // PDFViewer // // Created by kdanmobile_2 on 2022/12/15. // #import "PDFAddWaterMarkViewController.h" #import #import #import "PDFViewController.h" #import "PDFWatermarkTextView.h" #import "PDFWatermarkTextPreview.h" #import "PDFWatermarkImageView.h" #import "PDFWatermarkImagePreview.h" #import "PDFWatermarkDataModel.h" #import "PDFWatermarkEditView.h" #import "PDFWatermarkDrawView.h" #import "PDFWatermarkClipView.h" #import "Masonry.h" @interface PDFAddWaterMarkViewController () @property (nonatomic,assign) CGSize imageSize; @property (nonatomic,strong) UIImage *image; @property (nonatomic,strong) PDFWatermarkDataModel *dataModel; @property (nonatomic,strong) NSMutableArray *dataArray; @property (nonatomic,strong) CPDFDocument *document; @property (nonatomic,strong) UIAlertController *alertController; @property (nonatomic,assign) CGAffineTransform transform1; @property (nonatomic,assign) CGAffineTransform transform2; @property (nonatomic,assign) CGRect normalWatermarkRect; @property (nonatomic,strong) UILabel *shadowWaterLabel; @property (nonatomic,assign) CGRect normalImageWatermarkRect; @property (nonatomic,strong) UILabel *shadowImageWaterLabel; @property (retain, nonatomic) IBOutlet PDFWatermarkEditView *editView; @property (retain, nonatomic) IBOutlet UITableView *editTableView; @property (retain, nonatomic) IBOutlet UISegmentedControl *segmentedControl; @property (retain, nonatomic) IBOutlet UIImageView *textDocumentView; @property (retain, nonatomic) IBOutlet UIImageView *imageDocumentView; @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 UILabel *preLabel; @property (retain, nonatomic) IBOutlet UISlider *textOpacitySlider; @property (retain, nonatomic) IBOutlet UISlider *textScaleSlider; @property (retain, nonatomic) IBOutlet UIButton *textPageBtn; @property (retain, nonatomic) IBOutlet UISwitch *textTitlSwitch; @property (retain, nonatomic) IBOutlet UITextField *textVerticalField; @property (retain, nonatomic) IBOutlet UITextField *textHorizontalField; @property (retain, nonatomic) IBOutlet UIButton *textRotationBtn; @property (retain, nonatomic) IBOutlet UIView *textPreview; @property (retain, nonatomic) IBOutlet PDFWatermarkClipView *textClipView; @property (retain, nonatomic) IBOutlet PDFWatermarkDrawView *textDrawView; @property (retain, nonatomic) IBOutlet PDFWatermarkDrawView *imageDrawView; @property (retain, nonatomic) IBOutlet PDFWatermarkClipView *imageClipView; @property (retain, nonatomic) IBOutlet UIView *imagePreView; @property (retain, nonatomic) IBOutlet UIImageView *preImage; @property (retain, nonatomic) IBOutlet UISlider *imageOpacitySlider; @property (retain, nonatomic) IBOutlet UISlider *imageScaleSlider; @property (retain, nonatomic) IBOutlet UIButton *imagePageBtn; @property (retain, nonatomic) IBOutlet UISwitch *imageTitlSwitch; @property (retain, nonatomic) IBOutlet UITextField *imageVerticalField; @property (retain, nonatomic) IBOutlet UITextField *imageHorizontalField; @property (retain, nonatomic) IBOutlet UIButton *imageSelect; @property (retain, nonatomic) IBOutlet UIButton *imageRotationBtn; @property (retain, nonatomic) IBOutlet UIView *imageView; @property (retain, nonatomic) IBOutlet UIView *textView; @end @implementation PDFAddWaterMarkViewController #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)]; UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClick)]; UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelClick)]; self.navigationItem.rightBarButtonItems = @[doneBtn,editBtn,cancelBtn]; _textDataModel = [[PDFWatermarkDataModel alloc] init]; _imagedataModel = [[PDFWatermarkDataModel alloc] init]; [self createView]; [self initDataModel]; _shadowWaterLabel = [[UILabel alloc] init]; _shadowWaterLabel.text = _preLabel.text; _shadowImageWaterLabel = [[UILabel alloc] init]; // [self addConstraint]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _textHorizontalField.delegate = self; _textVerticalField.delegate = self; _imageHorizontalField.delegate = self; _imageVerticalField.delegate = self; [self createGestureRecognizer]; [self createImageGestureRecognizer]; } #pragma mark - Initializers - (void)initDataModel { self.textDataModel.textColor = UIColor.blackColor; self.textDataModel.watermarkOpacity = 1; self.textDataModel.text = @"Watermark"; self.textDataModel.watermarkScale = 17.0 / 24.0; self.textDataModel.isTile = NO; self.textDataModel.watermarkRotation = 0; self.textDataModel.verticalSpacing = 100; self.textDataModel.horizontalSpacing = 100; self.imagedataModel.textColor = UIColor.blackColor; self.imagedataModel.watermarkOpacity = 1; self.imagedataModel.watermarkScale = 17.0 / 24.0; self.imagedataModel.isTile = NO; self.imagedataModel.watermarkRotation = 0; self.imagedataModel.verticalSpacing = 100; self.imagedataModel.horizontalSpacing = 100; self.imagedataModel.imagePath = @"btn_selected"; self.imagedataModel.image = [UIImage imageNamed:self.imagedataModel.imagePath]; } - (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 = _image.size; } return self; } - (void)createView { [_segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; _imageDocumentView.image = _image; _textDocumentView.image = _image; [_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]; [_textRotationBtn setTitle:@" " forState:UIControlStateNormal]; [_imageRotationBtn setTitle:@" " forState:UIControlStateNormal]; // _editView = [[PDFWatermarkEditView alloc] init]; _editTableView.dataSource = self; _editTableView.delegate = self; _editTableView.backgroundColor = [UIColor systemGrayColor]; _editTableView.rowHeight = 50; _editTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _editTableView.tableFooterView = [[UIView alloc] init]; [_editTableView setEditing:YES animated:YES]; _editTableView.allowsMultipleSelection = NO; _editTableView.allowsSelectionDuringEditing = NO; _editTableView.allowsMultipleSelectionDuringEditing = NO; _imageViewController.hidden = YES; _textVerticalField.enabled = NO; _textHorizontalField.enabled = NO; _imageVerticalField.enabled = NO; _imageHorizontalField.enabled = NO; _textHorizontalField.text = @"100"; _textVerticalField.text = @"100"; _imageVerticalField.text = @"100"; _imageHorizontalField.text = @"100"; _alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Watermark Content", nil) message:nil preferredStyle:UIAlertControllerStyleAlert]; __block PDFAddWaterMarkViewController *strongBlock = self; [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }]; [_alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { strongBlock.textDataModel.text = strongBlock.alertController.textFields.firstObject.text; strongBlock.preLabel.text = strongBlock.alertController.textFields.firstObject.text; [strongBlock.preLabel sizeToFit]; }]]; [_alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { strongBlock.alertController.view.hidden = YES; }]]; } #pragma mark - UI Functions - (IBAction)onColorBtnClicked:(UIButton *)sender { self.preLabel.textColor = sender.backgroundColor; [self.textDataModel setTextColor:sender.backgroundColor]; } - (IBAction)onImageOpacityChanged:(UISlider *)sender { _preImage.alpha = 1 - sender.value; [self.textDataModel setWatermarkOpacity:1 - sender.value]; } - (IBAction)onOpacityChanged:(UISlider *)sender { _preLabel.alpha = 1 - sender.value; [self.textDataModel setWatermarkOpacity:1 - sender.value]; } - (IBAction)onImageScaleChanged:(UISlider *)sender { sender.minimumValue = 0.5; sender.maximumValue = 2; if (self.imagedataModel.watermarkRotation) { _preImage.transform = CGAffineTransformScale(self.transform1, sender.value, sender.value); } else { _preImage.transform = CGAffineTransformMakeScale(sender.value, sender.value); } _transform2 = CGAffineTransformMakeScale(sender.value, sender.value); _shadowImageWaterLabel.transform = CGAffineTransformMakeScale(sender.value, sender.value); [self.imagedataModel setWatermarkScale:sender.value]; _normalImageWatermarkRect = _shadowImageWaterLabel.frame; } - (IBAction)onTileSwitchChanged:(UISwitch *)sender { if ([sender isOn]) { self.textDataModel.isTile = YES; _textHorizontalField.enabled = YES; _textVerticalField .enabled = YES; _textScaleSlider.enabled = NO; _textDrawView.hidden = NO; _textClipView.hidden = NO; if (_shadowWaterLabel.frame.size.height == 0) { _shadowWaterLabel.frame = _preLabel.frame; } _normalWatermarkRect = _shadowWaterLabel.frame; [_textDrawView setDataModel:self.textDataModel]; [_textDrawView setWaterLabelRect:self.normalWatermarkRect]; [_textDrawView setDocumentViewRect:self.textDocumentView.frame]; [_textClipView setDocumentRect:_textDocumentView.frame]; [_textDrawView setNeedsDisplay]; [_textClipView setNeedsDisplay]; } else { self.textDataModel.isTile = NO; _textHorizontalField.enabled = NO; _textVerticalField.enabled = NO; _textScaleSlider.enabled = YES; _textDrawView.hidden = YES; _textClipView.hidden = YES; } } - (IBAction)onImageTileSwitchChanged:(UISwitch *)sender { if ([sender isOn]) { self.imagedataModel.isTile = YES; _imageHorizontalField.enabled = YES; _imageVerticalField.enabled = YES; _imageScaleSlider.enabled = NO; // _drawView = [[PDFWatermarkDrawView alloc] initWithFrame:self.imagePreview.bounds]; _imageDrawView.hidden = NO; _imageClipView.hidden = NO; if (_shadowImageWaterLabel.frame.size.height == 0) { _shadowImageWaterLabel.frame = _preImage.frame; _normalImageWatermarkRect = _shadowImageWaterLabel.frame; } [_imageDrawView setDataModel:self.imagedataModel]; [_imageDrawView setDocumentViewRect:self.imageDocumentView.frame]; [_imageDrawView setWaterLabelRect:self.normalImageWatermarkRect]; [_imageClipView setDocumentRect:_imageDocumentView.frame]; [_imageDrawView setNeedsDisplay]; [_imageClipView setNeedsDisplay]; } else { self.imagedataModel.isTile = NO; _imageHorizontalField.enabled = NO; _imageVerticalField.enabled = NO; _imageScaleSlider.enabled = YES; _imageDrawView.hidden = YES; _imageClipView.hidden = YES; } } - (IBAction)onTextScaleChanged:(UISlider *)sender { if (!self.textDataModel.isTile) { sender.minimumValue = 12; sender.maximumValue = 36; _preLabel.font = [_preLabel.font fontWithSize:sender.value]; self.shadowWaterLabel.font = [_preLabel.font fontWithSize:sender.value]; self.shadowWaterLabel.center = _preLabel.center; [_preLabel sizeToFit]; [self.shadowWaterLabel sizeToFit]; [self.textDataModel setWatermarkScale:sender.value / 24]; } self.normalWatermarkRect = self.shadowWaterLabel.frame; } - (IBAction)horizontalChange:(UITextField *)sender { [self.textDrawView setHorizontal:[sender.text floatValue]]; self.textDataModel.horizontalSpacing = [sender.text floatValue]; [self.textDrawView setNeedsDisplay]; } - (IBAction)verticalChage:(UITextField *)sender { [self.textDrawView setVertical:[sender.text floatValue]]; self.textDataModel.verticalSpacing = [sender.text floatValue]; [self.textDrawView setNeedsDisplay]; } - (IBAction)imageHorizontalChange:(UITextField *)sender { [_imageDrawView setHorizontal:[sender.text floatValue]]; self.imagedataModel.horizontalSpacing = [sender.text floatValue]; [_imageDrawView setNeedsDisplay]; } - (IBAction)imageVerticalChage:(UITextField *)sender { [_imageDrawView setVertical:[sender.text floatValue]]; self.imagedataModel.verticalSpacing = [sender.text floatValue]; [_imageDrawView setNeedsDisplay]; } - (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) { }]; UIAlertAction *customRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"Custom Page Range", 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.imagedataModel.pageString = alertController.textFields.firstObject.text; self.textDataModel.pageString = alertController.textFields.firstObject.text; }]]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } - (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]; } #pragma mark - Gesture - (void)createGestureRecognizer { [_textDocumentView setUserInteractionEnabled:YES]; [_preLabel setUserInteractionEnabled:YES]; [_textRotationBtn setUserInteractionEnabled:YES]; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWatermarkLabel:)]; [_preLabel addGestureRecognizer:tapGestureRecognizer]; UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkLabel:)]; [_preLabel addGestureRecognizer:panRecognizer]; UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotationWatermarkLabel:)]; [_textRotationBtn addGestureRecognizer:panRotationBtnRecognizer]; } - (void)tapWatermarkLabel:(UITapGestureRecognizer *)recognizer { _alertController.view.hidden = NO; _alertController.textFields.firstObject.text = self.preLabel.text; [self presentViewController:_alertController animated:true completion:nil]; } - (void)panWatermarkLabel:(UIPanGestureRecognizer *)recognizer { CGPoint point = [recognizer translationInView:_textPreview]; CGRect documentFrame = _textPreview.frame; documentFrame.origin.x -= _textPreview.frame.origin.x; documentFrame.origin.y -= _textPreview.frame.origin.y; [_preLabel setCenter:CGPointMake(_preLabel.center.x + point.x, _preLabel.center.y + point.y)]; [_textRotationBtn setCenter:CGPointMake(_textRotationBtn.center.x + point.x, _textRotationBtn.center.y + point.y)]; if (!CGRectContainsRect(documentFrame,_preLabel.frame)) { [_preLabel setCenter:CGPointMake(_preLabel.center.x - point.x, _preLabel.center.y - point.y)]; [_textRotationBtn setCenter:CGPointMake(_textRotationBtn.center.x - point.x, _textRotationBtn.center.y - point.y)]; } self.textDataModel.tx = _preLabel.center.x - (_textDocumentView.center.x - _textDocumentView.frame.origin.x); self.textDataModel.ty = _textDocumentView.center.y - _textDocumentView.frame.origin.y - _preLabel.center.y; [recognizer setTranslation:CGPointZero inView:_textPreview]; self.shadowWaterLabel.center = _preLabel.center; // self.normalWatermarkRect = self.shadowWaterLabel.frame; } - (void)rotationWatermarkLabel:(UIPanGestureRecognizer *)recognizer { CGPoint point = [recognizer translationInView:_textPreview]; CGFloat radian = atan2(point.x + _textPreview.center.x - _preLabel.center.x, point.y + _textPreview.center.y - _preLabel.center.y); _preLabel.transform = CGAffineTransformMakeRotation(-radian); self.textDataModel.watermarkRotation = (-radian) * 180 / M_PI; } - (void)createImageGestureRecognizer { [_imageDocumentView setUserInteractionEnabled:YES]; [_preImage setMultipleTouchEnabled:YES]; [_preImage setUserInteractionEnabled:YES]; [_imageRotationBtn setUserInteractionEnabled:YES]; UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panImageWatermarkView:)]; [_preImage addGestureRecognizer:panRecognizer]; UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImageWatermarkView:)]; [_imageRotationBtn addGestureRecognizer:panRotationBtnRecognizer]; } - (void)rotateImageWatermarkView:(UIPanGestureRecognizer *)recognizer { CGPoint point = [recognizer translationInView:_imagePreView]; CGFloat radian = atan2(point.x + _imagePreView.center.x - _preImage.center.x,point.y + _imagePreView.center.y - _preImage.center.y); if (self.imagedataModel.watermarkScale == (17.0 / 24.0)) { _preImage.transform = CGAffineTransformMakeRotation(-radian); } else { _preImage.transform = CGAffineTransformRotate(self.transform2, -radian); } _transform1 = CGAffineTransformMakeRotation(-radian); self.imagedataModel.watermarkRotation = (-radian) * 180 / M_PI; } - (void)panImageWatermarkView:(UIPanGestureRecognizer *)recognizer { CGPoint point = [recognizer translationInView:_imagePreView]; CGRect documentFrame = _imagePreView.frame; documentFrame.origin.x -= _imagePreView.frame.origin.x; documentFrame.origin.y -= _imagePreView.frame.origin.y; [_preImage setCenter:CGPointMake(_preImage.center.x + point.x, _preImage.center.y + point.y)]; [_imageRotationBtn setCenter:CGPointMake(_imageRotationBtn.center.x + point.x, _imageRotationBtn.center.y + point.y)]; if (!CGRectContainsRect(documentFrame,_preImage.frame)) { [_preImage setCenter:CGPointMake(_preImage.center.x - point.x, _preImage.center.y - point.y)]; [_imageRotationBtn setCenter:CGPointMake(_imageRotationBtn.center.x - point.x, _imageRotationBtn.center.y - point.y)]; } self.imagedataModel.tx = _preImage.center.x - (_imageDocumentView.center.x - _imageDocumentView.frame.origin.x); self.imagedataModel.ty = _imageDocumentView.center.y - _imageDocumentView.frame.origin.y - _preImage.center.y; _shadowImageWaterLabel.center = _preImage.center; [recognizer setTranslation:CGPointZero inView:_imagePreView]; _normalImageWatermarkRect = _shadowImageWaterLabel.frame; } #pragma mark - Accessors - (NSArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; NSArray *waterArray = [_document watermarks]; for (NSInteger i = 0; i < waterArray.count; i++) { CPDFWatermark *dataWater = [[CPDFWatermark alloc] init]; dataWater = waterArray[i]; [_dataArray addObject:dataWater]; } } return _dataArray; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *path = @"AddWater"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:path]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:path]; } if ([self.dataArray[indexPath.row] text]) { cell.textLabel.text = [self.dataArray[indexPath.row] text]; } else { cell.imageView.image = [self.dataArray[indexPath.row] image]; } return cell; } #pragma mark - UITableViewDelegate - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.delegate PDFViewPerformDeleteWatermark:self.dataArray[indexPath.row]]; [self.dataArray removeObjectAtIndex:indexPath.row]; [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { CPDFWatermark *dataWater = _dataArray[sourceIndexPath.row]; [_dataArray removeObject:dataWater]; [_dataArray insertObject:dataWater atIndex:destinationIndexPath.row]; } #pragma mark - Actions - (void)onClickedDoneBtn { self.navigationItem.rightBarButtonItem = nil; if (_segmentedControl.selectedSegmentIndex == 0) { _dataModel = self.textDataModel; [self.delegate PDFViewPerformChangeTextWatermark:self.dataModel]; } else { _dataModel = self.imagedataModel; [self.delegate PDFViewPerformChangeImageWatermark:self.dataModel]; } [self.navigationController popViewControllerAnimated:YES]; } - (void)segmentedAction:(UISegmentedControl *)sender { switch(sender.selectedSegmentIndex) { case 0: _textViewController.hidden = NO; _imageViewController.hidden = YES; break; case 1: _textViewController.hidden = YES; _imageViewController.hidden = NO; break; default: break; } } - (void)editClick { _editView.hidden = NO; } - (void)cancelClick { _editView.hidden = YES; } #pragma mark - CPDFClipTextPreviewDelegate - (void)clipText:(CGContextRef)context { CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor); CGContextFillRect(context, _textPreview.frame); CGContextClearRect(context, _textDocumentView.frame); } #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = info[UIImagePickerControllerOriginalImage]; CGSize size; size.width = image.size.width / 30; size.height = image.size.height / 30; _preImage.image = [self imageWithImageSimple:image scaledToSize:size]; self.imagedataModel.image = [self imageWithImageSimple:image scaledToSize:size]; [_preImage sizeToFit]; _shadowImageWaterLabel.frame = _preImage.frame; [picker dismissViewControllerAnimated:YES completion:nil]; _normalImageWatermarkRect = _shadowImageWaterLabel.frame; } - (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 - (void)textFieldDidBeginEditing:(UITextField *)textField { if (_imageHorizontalField == textField || _imageVerticalField == textField) { self.imageDrawView.hidden = YES; self.imageClipView.hidden = YES; if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) { } else { if (_imageView.frame.origin.y > (self.view.frame.size.height - 450)) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ { self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y - 300); } completion:nil]; } } } else if (_textHorizontalField == textField || _textVerticalField == textField) { self.textDrawView.hidden = YES; self.textClipView.hidden = YES; if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) { } else { if (_textView.frame.origin.y > (self.view.frame.size.height - 450)) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ { self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y - 300); } completion:nil]; } } } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; if (_imageHorizontalField == textField || _imageVerticalField == textField) { self.imageDrawView.hidden = NO; self.imageClipView.hidden = NO; if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) { } else { if(_imageView.frame.origin.y < self.view.frame.size.height - 200) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ { self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y + 300); } completion:nil]; } } } else if (_textHorizontalField == textField || _textVerticalField == textField) { self.imageDrawView.hidden = NO; self.imageClipView.hidden = NO; if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) { } else { if(_textView.frame.origin.y < self.view.frame.size.height - 200) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ { self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y + 300); } completion:nil]; } } } return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (_textHorizontalField == textField || _textVerticalField == textField) { return [self validateValue:string]; } else if (_imageHorizontalField == textField || _imageVerticalField == 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