CPDFTextViewController.m 15 KB

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