CPDFTextViewController.m 13 KB

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