PDFWatermarkDrawView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // CPDFDrawView.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2022/12/22.
  6. //
  7. #import "PDFWatermarkDrawView.h"
  8. @implementation PDFWatermarkDrawView
  9. - (instancetype)initWithFrame:(CGRect)frame {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. self.backgroundColor = [UIColor clearColor];
  13. _dataModel = [[PDFWatermarkDataModel alloc] init];
  14. _horizontal = 100;
  15. _vertical = 100;
  16. }
  17. return self;
  18. }
  19. - (void)drawRect:(CGRect)rect {
  20. [super drawRect:rect];
  21. CGContextRef context = UIGraphicsGetCurrentContext();
  22. if (self.dataModel.text) {
  23. [self drawText:context];
  24. } else if (self.dataModel.image) {
  25. [self drawImage:context];
  26. }
  27. }
  28. - (void)drawText:(CGContextRef)context {
  29. CGContextSetFillColorWithColor(context, self.dataModel.textColor.CGColor);
  30. CGContextSaveGState(context);
  31. CGContextSetAlpha(context, self.dataModel.watermarkOpacity);
  32. UIFont *font = [UIFont systemFontOfSize:20 * self.dataModel.watermarkScale];
  33. NSDictionary *dic = @{NSFontAttributeName:font,NSForegroundColorAttributeName:self.dataModel.textColor};
  34. NSInteger wx,wy,height,width;
  35. wx = self.waterLabelRect.origin.x + self.documentViewRect.origin.x;
  36. wy = self.waterLabelRect.origin.y + self.documentViewRect.origin.y;
  37. height = self.waterLabelRect.size.height + self.dataModel.verticalSpacing / 2.5;
  38. width = self.waterLabelRect.size.width + self.dataModel.horizontalSpacing / 2.1;
  39. NSInteger x,y,a,b;
  40. x = self.frame.size.width / width;
  41. y = self.frame.size.height / height;
  42. a = wx % width;
  43. b = wy % height;
  44. CGFloat ctm = (self.dataModel.watermarkRotation * M_PI) / 180;
  45. CGContextTranslateCTM(context, self.center.x, self.center.y);
  46. CGContextRotateCTM(context, ctm);
  47. for (NSInteger i = 0; i < x; i++) {
  48. for (NSInteger j = 0; j < y; j++) {
  49. [self.dataModel.text drawInRect:CGRectMake((a + i * width) - self.center.x, b + j * height - self.center.y, width - self.dataModel.horizontalSpacing / 2.5, height - self.dataModel.verticalSpacing / 2.5) withAttributes:dic];
  50. }
  51. }
  52. CGContextRotateCTM(context, -ctm);
  53. CGContextTranslateCTM(context, - self.center.x, - self.center.y);
  54. }
  55. - (void)drawImage:(CGContextRef)context {
  56. CGContextSaveGState(context);
  57. CGContextSetAlpha(context, self.dataModel.watermarkOpacity);
  58. NSInteger wx,wy,height,width;
  59. wx = self.waterLabelRect.origin.x + self.documentViewRect.origin.x;
  60. wy = self.waterLabelRect.origin.y + self.documentViewRect.origin.y;
  61. height = self.waterLabelRect.size.height + self.dataModel.verticalSpacing / 1.8;
  62. width = self.waterLabelRect.size.width + self.dataModel.horizontalSpacing / 1.8;
  63. NSInteger x,y,a,b;
  64. x = self.frame.size.width / width;
  65. y = self.frame.size.height / height;
  66. a = wx % width;
  67. b = wy % height;
  68. CGFloat ctm = (self.dataModel.watermarkRotation * M_PI) / 180;
  69. CGContextTranslateCTM(context, self.center.x, self.center.y);
  70. CGContextRotateCTM(context, ctm);
  71. for (NSInteger i = 0; i < x; i++) {
  72. for (NSInteger j = 0; j < y; j++) {
  73. [self.dataModel.image drawInRect:CGRectMake((a + i * width) - self.center.x, b + j * height - self.center.y, width - self.dataModel.horizontalSpacing / 1.8, height - self.dataModel.verticalSpacing / 1.8) blendMode:kCGBlendModeNormal alpha:self.dataModel.watermarkOpacity];
  74. }
  75. }
  76. CGContextRotateCTM(context, -ctm);
  77. CGContextTranslateCTM(context, - self.center.x, - self.center.y);
  78. }
  79. @end