123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // CPDFDrawView.m
- // PDFViewer
- //
- // Created by kdanmobile_2 on 2022/12/22.
- //
- #import "PDFWatermarkDrawView.h"
- @implementation PDFWatermarkDrawView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
-
- if (self) {
- self.backgroundColor = [UIColor clearColor];
-
- _dataModel = [[PDFWatermarkDataModel alloc] init];
-
- _horizontal = 100;
- _vertical = 100;
- }
-
- return self;
- }
- - (void)drawRect:(CGRect)rect {
- [super drawRect:rect];
-
- CGContextRef context = UIGraphicsGetCurrentContext();
- if (self.dataModel.text) {
- [self drawText:context];
- } else if (self.dataModel.image) {
- [self drawImage:context];
- }
- }
- - (void)drawText:(CGContextRef)context {
- CGContextSetFillColorWithColor(context, self.dataModel.textColor.CGColor);
- CGContextSaveGState(context);
-
- CGContextSetAlpha(context, self.dataModel.watermarkOpacity);
-
- UIFont *font = [UIFont systemFontOfSize:20 * self.dataModel.watermarkScale];
- NSDictionary *dic = @{NSFontAttributeName:font,NSForegroundColorAttributeName:self.dataModel.textColor};
-
- NSInteger wx,wy,height,width;
- wx = self.waterLabelRect.origin.x + self.documentViewRect.origin.x;
- wy = self.waterLabelRect.origin.y + self.documentViewRect.origin.y;
- height = self.waterLabelRect.size.height + self.dataModel.verticalSpacing / 2.5;
- width = self.waterLabelRect.size.width + self.dataModel.horizontalSpacing / 2.1;
- NSInteger x,y,a,b;
- x = self.frame.size.width / width;
- y = self.frame.size.height / height;
- a = wx % width;
- b = wy % height;
-
- CGFloat ctm = (self.dataModel.watermarkRotation * M_PI) / 180;
-
- CGContextTranslateCTM(context, self.center.x, self.center.y);
-
- CGContextRotateCTM(context, ctm);
- for (NSInteger i = 0; i < x; i++) {
- for (NSInteger j = 0; j < y; j++) {
-
- [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];
- }
- }
-
- CGContextRotateCTM(context, -ctm);
- CGContextTranslateCTM(context, - self.center.x, - self.center.y);
- }
- - (void)drawImage:(CGContextRef)context {
- CGContextSaveGState(context);
-
- CGContextSetAlpha(context, self.dataModel.watermarkOpacity);
-
- NSInteger wx,wy,height,width;
- wx = self.waterLabelRect.origin.x + self.documentViewRect.origin.x;
- wy = self.waterLabelRect.origin.y + self.documentViewRect.origin.y;
- height = self.waterLabelRect.size.height + self.dataModel.verticalSpacing / 1.8;
- width = self.waterLabelRect.size.width + self.dataModel.horizontalSpacing / 1.8;
- NSInteger x,y,a,b;
- x = self.frame.size.width / width;
- y = self.frame.size.height / height;
- a = wx % width;
- b = wy % height;
-
- CGFloat ctm = (self.dataModel.watermarkRotation * M_PI) / 180;
-
- CGContextTranslateCTM(context, self.center.x, self.center.y);
-
- CGContextRotateCTM(context, ctm);
- for (NSInteger i = 0; i < x; i++) {
- for (NSInteger j = 0; j < y; j++) {
-
- [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];
- }
- }
- CGContextRotateCTM(context, -ctm);
-
- CGContextTranslateCTM(context, - self.center.x, - self.center.y);
- }
- @end
|