PDFTextViewController.m 16 KB

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