|
@@ -13,6 +13,8 @@
|
|
|
|
|
|
@interface CPDFTextViewController ()
|
|
|
|
|
|
+@property (nonatomic,assign) CGRect watermarkFrame;
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
@implementation CPDFTextViewController
|
|
@@ -24,20 +26,79 @@
|
|
|
_textPreview = [[CPDFTextPreview alloc] init];
|
|
|
_textView = [[CPDFTextView alloc] init];
|
|
|
_dataModel = [[CPDFDataModel alloc] init];
|
|
|
+ _alertController = [UIAlertController alertControllerWithTitle:@"Watermark Content" message:nil preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
[self.view addSubview:_textPreview];
|
|
|
[self.view addSubview:_textView];
|
|
|
+ [self.view setBackgroundColor:UIColor.systemGray5Color];
|
|
|
|
|
|
- UIEdgeInsets previewPadding = UIEdgeInsetsMake(0, 0, self.view.bounds.size.height / 3, 0);
|
|
|
- [_textPreview mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
- make.edges.equalTo(self.view).with.insets(previewPadding);
|
|
|
- }];
|
|
|
-
|
|
|
- UIEdgeInsets textViewPadding = UIEdgeInsetsMake(430, 0, 0, 0);
|
|
|
- [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
- make.edges.equalTo(self.view).with.insets(textViewPadding);
|
|
|
- }];
|
|
|
+ [self addConstraint];
|
|
|
+ [self initDataModel];
|
|
|
[self addTargets];
|
|
|
+ [self createGestureRecognizer];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
|
|
+ [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
|
|
|
+
|
|
|
+ if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
|
|
|
+
|
|
|
+ [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.right.equalTo(self.view.mas_right).offset(-30);
|
|
|
+ make.left.equalTo(_textPreview.mas_right).offset(0);
|
|
|
+ make.height.equalTo(@205);
|
|
|
+ make.width.equalTo(@(self.view.bounds.size.width));
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(-55);
|
|
|
+ }];
|
|
|
+ [_textPreview mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(self.view.mas_top).offset(10);
|
|
|
+ make.right.equalTo(_textView.mas_left).offset(-5);
|
|
|
+ make.left.equalTo(self.view.mas_left).offset(0);
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(0);
|
|
|
+ }];
|
|
|
+
|
|
|
+ } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
|
|
|
+ [_textPreview mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(self.view.mas_top).offset(0);
|
|
|
+ make.width.equalTo(self.view.mas_width);
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(-205);
|
|
|
+ }];
|
|
|
+
|
|
|
+ [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(_textPreview.mas_bottom).offset(0);
|
|
|
+ make.width.equalTo(_textPreview.mas_width);
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(0);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)initDataModel {
|
|
|
+ _dataModel.textColor = UIColor.blackColor;
|
|
|
+ _dataModel.watermarkOpacity = 1;
|
|
|
+ _dataModel.text = @"Watermark";
|
|
|
+ _dataModel.watermarkScale = 1;
|
|
|
+ _dataModel.isTile = NO;
|
|
|
+ _dataModel.watermarkRotation = 0;
|
|
|
+ _textPreview.watermarkLabel.text = _dataModel.text;
|
|
|
+ _textView.horizontalField.text = 0;
|
|
|
+ _textView.verticalField.text = 0;
|
|
|
+ _textView.horizontalField.enabled = NO;
|
|
|
+ _textView.verticalField.enabled = NO;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)addConstraint {
|
|
|
+ [_textPreview mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(self.view.mas_top).offset(0);
|
|
|
+ make.width.equalTo(self.view.mas_width);
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(-205);
|
|
|
+ }];
|
|
|
+
|
|
|
+ [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(_textPreview.mas_bottom).offset(0);
|
|
|
+ make.width.equalTo(_textPreview.mas_width);
|
|
|
+ make.bottom.equalTo(self.view.mas_bottom).offset(0);
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
- (void)addTargets {
|
|
@@ -46,9 +107,40 @@
|
|
|
[_textView.colorArray[i] addTarget:self action:@selector(onColorBtnClicked:) forControlEvents:UIControlEventTouchDown];
|
|
|
}
|
|
|
|
|
|
+ __block CPDFTextViewController *strongBlock = self;
|
|
|
+ [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
|
|
|
+
|
|
|
+ }];
|
|
|
+ [_alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+
|
|
|
+ strongBlock.dataModel.text = strongBlock.alertController.textFields.firstObject.text;
|
|
|
+ strongBlock.textPreview.watermarkLabel.text = strongBlock.alertController.textFields.firstObject.text;
|
|
|
+ [strongBlock watermarkAdaption];
|
|
|
+
|
|
|
+ }]];
|
|
|
+ [_alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ strongBlock.alertController.view.hidden = YES;
|
|
|
+ }]];
|
|
|
+
|
|
|
[_textView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
|
|
|
[_textView.textScaleSlider addTarget:self action:@selector(onTextScaleChanged:) forControlEvents:UIControlEventValueChanged];
|
|
|
[_textView.tileSwitch addTarget:self action:@selector(onTileSwitchChanged:) forControlEvents:UIControlEventValueChanged];
|
|
|
+ [_textView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchDown];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)createGestureRecognizer {
|
|
|
+ [_textPreview.documentView setUserInteractionEnabled:YES];
|
|
|
+ [_textPreview.watermarkLabel setUserInteractionEnabled:YES];
|
|
|
+ [_textPreview.rotationBtn setUserInteractionEnabled:YES];
|
|
|
+
|
|
|
+ UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWatermarkLabel:)];
|
|
|
+ [_textPreview.watermarkLabel addGestureRecognizer:tapGestureRecognizer];
|
|
|
+
|
|
|
+ UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkLabel:)];
|
|
|
+ [_textPreview.watermarkLabel addGestureRecognizer:panRecognizer];
|
|
|
+
|
|
|
+ UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotationWatermarkLabel:)];
|
|
|
+ [_textPreview.rotationBtn addGestureRecognizer:panRotationBtnRecognizer];
|
|
|
}
|
|
|
|
|
|
- (void)onColorBtnClicked:(UIButton *)sender {
|
|
@@ -62,24 +154,103 @@
|
|
|
}
|
|
|
|
|
|
- (void)onTextScaleChanged:(UISlider *)sender {
|
|
|
- sender.minimumValue = 24;
|
|
|
- sender.maximumValue = 70;
|
|
|
- _textPreview.watermarkLabel.font = [_textPreview.watermarkLabel.font fontWithSize:sender.value];
|
|
|
- [_textPreview.watermarkLabel sizeToFit];
|
|
|
- CGRect frame = _textPreview.watermarkLabel.frame;
|
|
|
- frame.origin = CGPointMake(_textPreview.bounds.size.width / 2 - _textPreview.watermarkLabel.frame.size.width / 2, _textPreview.bounds.size.height / 2 - _textPreview.watermarkLabel.frame.size.height / 2);
|
|
|
- _textPreview.watermarkLabel.frame = frame;
|
|
|
-
|
|
|
- _textPreview.rotationBtn.frame = CGRectMake(_textPreview.bounds.size.width / 2 + _textPreview.watermarkLabel.frame.size.width / 2 - 10, _textPreview.bounds.size.height / 2 + _textPreview.watermarkLabel.frame.size.height / 2 - 10, 20, 20);
|
|
|
- [_dataModel setWatermarkScale:sender.value / 24];
|
|
|
+ if (!_dataModel.isTile) {
|
|
|
+ sender.minimumValue = 24;
|
|
|
+ sender.maximumValue = 48;
|
|
|
+ _textPreview.watermarkLabel.font = [_textPreview.watermarkLabel.font fontWithSize:sender.value];
|
|
|
+ [self watermarkAdaption];
|
|
|
+ [_dataModel setWatermarkScale:sender.value / 24];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- (void)onTileSwitchChanged:(UISwitch *) sender {
|
|
|
if ([sender isOn]) {
|
|
|
_dataModel.isTile = YES;
|
|
|
+ _textView.horizontalField.enabled = YES;
|
|
|
+ _textView.verticalField.enabled = YES;
|
|
|
+ _textView.textScaleSlider.enabled = NO;
|
|
|
} else {
|
|
|
_dataModel.isTile = NO;
|
|
|
+ _textView.horizontalField.enabled = NO;
|
|
|
+ _textView.verticalField.enabled = NO;
|
|
|
+ _textView.textScaleSlider.enabled = YES;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)onSelectPageRange:(UIButton *)sender {
|
|
|
+ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
|
|
|
+ UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ self.textView.rangeLabel.text = @"Page Range: ALL";
|
|
|
+ }];
|
|
|
+ UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom Page Range" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ [self createCustomRangeAlert];
|
|
|
+ }];
|
|
|
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 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:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+
|
|
|
+ [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
|
|
|
+ textField.placeholder = @"such as:1,3-5,10";
|
|
|
+ }];
|
|
|
+ [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+
|
|
|
+ self.dataModel.pageString = alertController.textFields.firstObject.text;
|
|
|
+ self.textView.rangeLabel.text = @"Page Range:Custom";
|
|
|
+ }]];
|
|
|
+ [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ }]];
|
|
|
+ [self presentViewController:alertController animated:YES completion:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)watermarkAdaption {
|
|
|
+ [_textPreview.watermarkLabel sizeToFit];
|
|
|
+
|
|
|
+ [_textPreview.rotationBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(_textPreview.watermarkLabel.mas_bottom).offset(-10);
|
|
|
+ make.width.equalTo(@20);
|
|
|
+ make.height.equalTo(@20);
|
|
|
+ make.left.equalTo(_textPreview.watermarkLabel.mas_right).offset(-10);
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)tapWatermarkLabel:(UITapGestureRecognizer *)recognizer {
|
|
|
+ _alertController.view.hidden = NO;
|
|
|
+ _alertController.textFields.firstObject.text = _textPreview.watermarkLabel.text;
|
|
|
+ [self presentViewController:_alertController animated:true completion:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)panWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
|
|
|
+ CGPoint point = [recognizer translationInView:_textPreview.documentView];
|
|
|
+ CGRect documentFrame = _textPreview.documentView.frame;
|
|
|
+ documentFrame.origin.x -= _textPreview.documentView.frame.origin.x;
|
|
|
+ documentFrame.origin.y -= _textPreview.documentView.frame.origin.y;
|
|
|
+ [_textPreview.watermarkLabel setCenter:CGPointMake(_textPreview.watermarkLabel.center.x + point.x, _textPreview.watermarkLabel.center.y + point.y)];
|
|
|
+ [_textPreview.rotationBtn setCenter:CGPointMake(_textPreview.rotationBtn.center.x + point.x, _textPreview.rotationBtn.center.y + point.y)];
|
|
|
+ if (!CGRectContainsRect(documentFrame,_textPreview.watermarkLabel.frame)) {
|
|
|
+ [_textPreview.watermarkLabel setCenter:CGPointMake(_textPreview.watermarkLabel.center.x - point.x, _textPreview.watermarkLabel.center.y - point.y)];
|
|
|
+ [_textPreview.rotationBtn setCenter:CGPointMake(_textPreview.rotationBtn.center.x - point.x, _textPreview.rotationBtn.center.y - point.y)];
|
|
|
}
|
|
|
+
|
|
|
+ _dataModel.tx = _textPreview.watermarkLabel.frame.origin.x - (_textPreview.documentView.center.x - _textPreview.documentView.frame.origin.x);
|
|
|
+ _dataModel.ty = _textPreview.documentView.center.y - _textPreview.documentView.frame.origin.y - _textPreview.watermarkLabel.frame.origin.y;
|
|
|
+ [recognizer setTranslation:CGPointZero inView:_textPreview.documentView];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)rotationWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
|
|
|
+
|
|
|
+ CGPoint point = [recognizer translationInView:_textPreview];
|
|
|
+ CGFloat radian = atan2(point.x + _textPreview.center.x - _textPreview.watermarkLabel.center.x,point.y + _textPreview.center.y - _textPreview.watermarkLabel.center.y);
|
|
|
+ _textPreview.watermarkLabel.transform = CGAffineTransformMakeRotation(-radian);
|
|
|
+
|
|
|
+ _dataModel.watermarkRotation = (-radian) * 180 / M_PI;
|
|
|
}
|
|
|
|
|
|
@end
|