CPDFTextViewController.m 17 KB

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