PDFTextWatermarkController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // CPDFTextViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/19.
  6. //
  7. #import "PDFTextWatermarkController.h"
  8. #import "PDFWatermarkTextView.h"
  9. #import "PDFWatermarkDataModel.h"
  10. #import "PDFWatermarkDrawView.h"
  11. #import "PDFWatermarkClipView.h"
  12. #import "Masonry.h"
  13. #import "PDFWatermarkControllerHeader.h"
  14. @interface PDFTextWatermarkController () <UITextFieldDelegate>
  15. @property (nonatomic,strong) UIAlertController *alertController;
  16. @property (nonatomic,strong) PDFWatermarkDrawView *drawView;
  17. @property (nonatomic,strong) PDFWatermarkClipView *cliView;
  18. @property (nonatomic,assign) CGRect normalWatermarkRect;
  19. @property (nonatomic,strong) UILabel *shadowWaterLabel;
  20. @end
  21. @implementation PDFTextWatermarkController
  22. #pragma mark - UIViewController Methods
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. _textPreview = [[PDFWatermarkTextPreview alloc] init];
  27. _textView = [[PDFWatermarkTextView alloc] init];
  28. _textView.verticalField.delegate = self;
  29. _textView.horizontalField.delegate = self;
  30. _alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Watermark Content", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  31. [self.view addSubview:_textPreview];
  32. [self.view addSubview:_textView];
  33. [self.view setBackgroundColor:UIColor.grayColor];
  34. [self addConstraint];
  35. [self initWatermarkView];
  36. [self addTargets];
  37. [self createGestureRecognizer];
  38. _shadowWaterLabel = [[UILabel alloc] init];
  39. _shadowWaterLabel.text = _textPreview.preLabel.text;
  40. }
  41. #pragma mark - Initializers
  42. - (void)initDataModel {
  43. self.dataModel.textColor = UIColor.blackColor;
  44. self.dataModel.watermarkOpacity = 1;
  45. self.dataModel.text = @"Watermark";
  46. self.dataModel.watermarkScale = 17.0 / 24.0;
  47. self.dataModel.isTile = NO;
  48. self.dataModel.watermarkRotation = 0;
  49. self.dataModel.verticalSpacing = 100;
  50. self.dataModel.horizontalSpacing = 100;
  51. }
  52. - (void)initWatermarkView {
  53. _textPreview.preLabel.text = self.dataModel.text;
  54. _textView.horizontalField.text = 0;
  55. _textView.verticalField.text = 0;
  56. _textView.horizontalField.enabled = NO;
  57. _textView.verticalField.enabled = NO;
  58. }
  59. - (void)addConstraint {
  60. [_textPreview mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.top.equalTo(self.view.mas_top).offset(0);
  62. make.width.equalTo(self.view.mas_width);
  63. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  64. }];
  65. [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.top.equalTo(_textPreview.mas_bottom).offset(0);
  67. make.width.equalTo(_textPreview.mas_width);
  68. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  69. }];
  70. }
  71. - (void)addTargets {
  72. for (NSInteger i = 0; i < _textView.colorArray.count; ++i) {
  73. [_textView.colorArray[i] addTarget:self action:@selector(onColorBtnClicked:) forControlEvents:UIControlEventTouchDown];
  74. }
  75. __block PDFTextWatermarkController *strongBlock = self;
  76. [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  77. }];
  78. [_alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  79. strongBlock.dataModel.text = strongBlock.alertController.textFields.firstObject.text;
  80. strongBlock.textPreview.preLabel.text = strongBlock.alertController.textFields.firstObject.text;
  81. [strongBlock.textPreview.preLabel sizeToFit];
  82. }]];
  83. [_alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  84. strongBlock.alertController.view.hidden = YES;
  85. }]];
  86. [_textView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
  87. [_textView.textScaleSlider addTarget:self action:@selector(onTextScaleChanged:) forControlEvents:UIControlEventValueChanged];
  88. [_textView.tileSwitch addTarget:self action:@selector(onTileSwitchChanged:) forControlEvents:UIControlEventValueChanged];
  89. [_textView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchDown];
  90. [_textView.horizontalField addTarget:self action:@selector(horizontalChange:) forControlEvents:UIControlEventEditingDidEnd];
  91. [_textView.verticalField addTarget:self action:@selector(verticalChage:) forControlEvents:UIControlEventEditingDidEnd];
  92. }
  93. #pragma mark - UI Functions
  94. - (void)onColorBtnClicked:(UIButton *)sender {
  95. _textPreview.preLabel.textColor = sender.backgroundColor;
  96. [self.dataModel setTextColor:sender.backgroundColor];
  97. }
  98. - (void)onOpacityChanged:(UISlider *)sender {
  99. _textPreview.preLabel.alpha = 1 - sender.value;
  100. [self.dataModel setWatermarkOpacity:1 - sender.value];
  101. }
  102. - (void)onTextScaleChanged:(UISlider *)sender {
  103. if (!self.dataModel.isTile) {
  104. sender.minimumValue = 12;
  105. sender.maximumValue = 36;
  106. _textPreview.preLabel.font = [_textPreview.preLabel.font fontWithSize:sender.value];
  107. self.shadowWaterLabel.font = [_textPreview.preLabel.font fontWithSize:sender.value];
  108. self.shadowWaterLabel.center = _textPreview.preLabel.center;
  109. [_textPreview.preLabel sizeToFit];
  110. [self.shadowWaterLabel sizeToFit];
  111. [self.dataModel setWatermarkScale:sender.value / 24];
  112. }
  113. self.normalWatermarkRect = self.shadowWaterLabel.frame;
  114. }
  115. - (void)onTileSwitchChanged:(UISwitch *) sender {
  116. if ([sender isOn]) {
  117. self.dataModel.isTile = YES;
  118. _textView.horizontalField.enabled = YES;
  119. _textView.verticalField.enabled = YES;
  120. _textView.textScaleSlider.enabled = NO;
  121. self.drawView = [[PDFWatermarkDrawView alloc] initWithFrame:self.textPreview.bounds];
  122. if (_shadowWaterLabel.frame.size.height == 0) {
  123. _shadowWaterLabel.frame = _textPreview.preLabel.frame;
  124. }
  125. _normalWatermarkRect = _shadowWaterLabel.frame;
  126. [self.drawView setDataModel:self.dataModel];
  127. [self.drawView setWaterLabelRect:self.normalWatermarkRect];
  128. [self.drawView setDocumentViewRect:self.textPreview.documentImageView.frame];
  129. [self.view addSubview:self.drawView];
  130. [self.textPreview bringSubviewToFront:self.drawView];
  131. self.cliView = [[PDFWatermarkClipView alloc] initWithFrame:self.textPreview.bounds];
  132. [self.cliView setDocumentRect:self.textPreview.documentImageView.frame];
  133. [self.view addSubview:self.cliView];
  134. [self.drawView bringSubviewToFront:self.cliView];
  135. } else {
  136. self.dataModel.isTile = NO;
  137. _textView.horizontalField.enabled = NO;
  138. _textView.verticalField.enabled = NO;
  139. _textView.textScaleSlider.enabled = YES;
  140. [self.drawView removeFromSuperview];
  141. [self.cliView removeFromSuperview];
  142. }
  143. }
  144. - (void)horizontalChange:(UITextField *)text {
  145. [self.drawView setHorizontal:[text.text floatValue]];
  146. self.dataModel.horizontalSpacing = [text.text floatValue];
  147. [self.drawView setNeedsDisplay];
  148. }
  149. - (void)verticalChage:(UITextField *)text {
  150. [self.drawView setVertical:[text.text floatValue]];
  151. self.dataModel.verticalSpacing = [text.text floatValue];
  152. [self.drawView setNeedsDisplay];
  153. }
  154. #pragma mark - Gesture
  155. - (void)createGestureRecognizer {
  156. [_textPreview.documentImageView setUserInteractionEnabled:YES];
  157. [_textPreview.preLabel setUserInteractionEnabled:YES];
  158. [_textPreview.rotationBtn setUserInteractionEnabled:YES];
  159. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWatermarkLabel:)];
  160. [_textPreview.preLabel addGestureRecognizer:tapGestureRecognizer];
  161. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkLabel:)];
  162. [_textPreview.preLabel addGestureRecognizer:panRecognizer];
  163. UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotationWatermarkLabel:)];
  164. [_textPreview.rotationBtn addGestureRecognizer:panRotationBtnRecognizer];
  165. }
  166. - (void)tapWatermarkLabel:(UITapGestureRecognizer *)recognizer {
  167. _alertController.view.hidden = NO;
  168. _alertController.textFields.firstObject.text = _textPreview.preLabel.text;
  169. [self presentViewController:_alertController animated:true completion:nil];
  170. }
  171. - (void)panWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
  172. CGPoint point = [recognizer translationInView:_textPreview.documentImageView];
  173. CGRect documentFrame = _textPreview.documentImageView.frame;
  174. documentFrame.origin.x -= _textPreview.documentImageView.frame.origin.x;
  175. documentFrame.origin.y -= _textPreview.documentImageView.frame.origin.y;
  176. [_textPreview.preLabel setCenter:CGPointMake(_textPreview.preLabel.center.x + point.x, _textPreview.preLabel.center.y + point.y)];
  177. [_textPreview.rotationBtn setCenter:CGPointMake(_textPreview.rotationBtn.center.x + point.x, _textPreview.rotationBtn.center.y + point.y)];
  178. if (!CGRectContainsRect(documentFrame,_textPreview.preLabel.frame)) {
  179. [_textPreview.preLabel setCenter:CGPointMake(_textPreview.preLabel.center.x - point.x, _textPreview.preLabel.center.y - point.y)];
  180. [_textPreview.rotationBtn setCenter:CGPointMake(_textPreview.rotationBtn.center.x - point.x, _textPreview.rotationBtn.center.y - point.y)];
  181. }
  182. self.dataModel.tx = _textPreview.preLabel.center.x - (_textPreview.documentImageView.center.x - _textPreview.documentImageView.frame.origin.x);
  183. self.dataModel.ty = _textPreview.documentImageView.center.y - _textPreview.documentImageView.frame.origin.y - _textPreview.preLabel.center.y;
  184. [recognizer setTranslation:CGPointZero inView:_textPreview.documentImageView];
  185. self.shadowWaterLabel.center = _textPreview.preLabel.center;
  186. self.normalWatermarkRect = self.shadowWaterLabel.frame;
  187. }
  188. - (void)rotationWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
  189. CGPoint point = [recognizer translationInView:_textPreview];
  190. CGFloat radian = atan2(point.x + _textPreview.center.x - _textPreview.preLabel.center.x, point.y + _textPreview.center.y - _textPreview.preLabel.center.y);
  191. _textPreview.preLabel.transform = CGAffineTransformMakeRotation(-radian);
  192. self.dataModel.watermarkRotation = (-radian) * 180 / M_PI;
  193. }
  194. #pragma mark - Orientation
  195. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  196. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  197. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
  198. [self addSideConstraint];
  199. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  200. [self addNormalConstraint];
  201. }
  202. if ([self.drawView isDescendantOfView:self.view]) {
  203. _textView.tileSwitch.on = NO;
  204. [self.cliView removeFromSuperview];
  205. [self.drawView removeFromSuperview];
  206. }
  207. }
  208. - (void)addSideConstraint {
  209. [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  210. make.right.equalTo(self.view.mas_right).offset(-38);
  211. make.left.equalTo(_textPreview.mas_right).offset(0);
  212. make.height.equalTo(@205);
  213. make.width.equalTo(@(self.view.bounds.size.width));
  214. make.bottom.equalTo(self.view.mas_bottom).offset(-40);
  215. }];
  216. [_textPreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  217. make.top.equalTo(self.view.mas_top).offset(0);
  218. make.right.equalTo(_textView.mas_left).offset(-5);
  219. make.left.equalTo(self.view.mas_left).offset(0);
  220. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  221. }];
  222. if ([self.drawView isDescendantOfView:self.view]) {
  223. [self.drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
  224. make.top.equalTo(self.view.mas_top).offset(0);
  225. make.right.equalTo(_textView.mas_left).offset(-5);
  226. make.left.equalTo(self.view.mas_left).offset(0);
  227. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  228. }];
  229. [self.cliView mas_remakeConstraints:^(MASConstraintMaker *make) {
  230. make.top.equalTo(self.view.mas_top).offset(0);
  231. make.right.equalTo(_textView.mas_left).offset(-5);
  232. make.left.equalTo(self.view.mas_left).offset(0);
  233. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  234. }];
  235. }
  236. }
  237. - (void)addNormalConstraint {
  238. [_textPreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  239. make.top.equalTo(self.view.mas_top).offset(0);
  240. make.width.equalTo(self.view.mas_width);
  241. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  242. }];
  243. [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  244. make.top.equalTo(_textPreview.mas_bottom).offset(0);
  245. make.width.equalTo(_textPreview.mas_width);
  246. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  247. }];
  248. if ([self.drawView isDescendantOfView:self.view]) {
  249. [self.drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
  250. make.top.equalTo(self.view.mas_top).offset(0);
  251. make.width.equalTo(self.view.mas_width);
  252. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  253. }];
  254. [self.cliView mas_remakeConstraints:^(MASConstraintMaker *make) {
  255. make.top.equalTo(self.view.mas_top).offset(0);
  256. make.width.equalTo(self.view.mas_width);
  257. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  258. }];
  259. }
  260. }
  261. #pragma mark - CPDFClipTextPreviewDelegate
  262. - (void)clipText:(CGContextRef)context {
  263. CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
  264. CGContextFillRect(context, self.textPreview.bounds);
  265. CGContextClearRect(context, self.textPreview.documentImageView.frame);
  266. }
  267. #pragma mark - UITextFieldDelegate
  268. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  269. self.cliView.hidden = YES;
  270. self.drawView.hidden = YES;
  271. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  272. } else {
  273. if (_textView.frame.origin.y > (self.view.frame.size.height - 300)) {
  274. [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  275. make.height.mas_equalTo(205);
  276. make.width.equalTo(_textPreview.mas_width);
  277. make.bottom.equalTo(self.view.mas_bottom).offset(-300);
  278. }];
  279. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  280. self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y - 300);
  281. } completion:nil];
  282. }
  283. }
  284. }
  285. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  286. [textField resignFirstResponder];
  287. self.cliView.hidden = NO;
  288. self.drawView.hidden = NO;
  289. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  290. } else {
  291. if(_textView.frame.origin.y < self.view.frame.size.height) {
  292. [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  293. make.top.equalTo(_textPreview.mas_bottom).offset(0);
  294. make.width.equalTo(_textPreview.mas_width);
  295. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  296. make.height.mas_equalTo(205);
  297. }];
  298. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  299. self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y + 300);
  300. } completion:nil];
  301. }
  302. }
  303. return YES;
  304. }
  305. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  306. if (_textView.horizontalField == textField || _textView.verticalField == textField) {
  307. return [self validateValue:string];
  308. }
  309. return YES;
  310. }
  311. @end