Browse Source

PDFview(ios) - 水印12/22

yangliuhua 2 years ago
parent
commit
894a01427b

+ 6 - 0
PDFViewer.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		C96150D2295400B00020F445 /* CPDFDrawView.m in Sources */ = {isa = PBXBuildFile; fileRef = C96150D1295400B00020F445 /* CPDFDrawView.m */; };
 		C96384932949E2020055CCCA /* ComPDFKit.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C96384922949E2020055CCCA /* ComPDFKit.xcframework */; };
 		C9638496294ACC240055CCCA /* CPDFAddViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9638495294ACC240055CCCA /* CPDFAddViewController.m */; };
 		C96D13932924793800CC92C7 /* btn_more.png in Resources */ = {isa = PBXBuildFile; fileRef = C96D13922924793800CC92C7 /* btn_more.png */; };
@@ -54,6 +55,8 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		C96150D0295400B00020F445 /* CPDFDrawView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFDrawView.h; sourceTree = "<group>"; };
+		C96150D1295400B00020F445 /* CPDFDrawView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFDrawView.m; sourceTree = "<group>"; };
 		C96384922949E2020055CCCA /* ComPDFKit.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = ComPDFKit.xcframework; sourceTree = "<group>"; };
 		C9638494294ACC240055CCCA /* CPDFAddViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFAddViewController.h; sourceTree = "<group>"; };
 		C9638495294ACC240055CCCA /* CPDFAddViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFAddViewController.m; sourceTree = "<group>"; };
@@ -218,6 +221,8 @@
 				ECD0ABE52928ED990091B6EB /* CPDFTextPreview.m */,
 				ECD0ABE92928ED990091B6EB /* CPDFTextView.h */,
 				ECD0ABEA2928ED990091B6EB /* CPDFTextView.m */,
+				C96150D0295400B00020F445 /* CPDFDrawView.h */,
+				C96150D1295400B00020F445 /* CPDFDrawView.m */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -337,6 +342,7 @@
 				ECC8831D2923352B00F6392E /* CPDFViewController.m in Sources */,
 				EC62979429220F8300D339EE /* ViewController.m in Sources */,
 				ECD0AC172928EECB0091B6EB /* MASConstraint.m in Sources */,
+				C96150D2295400B00020F445 /* CPDFDrawView.m in Sources */,
 				EC6297B8292239A700D339EE /* CPDFModel.m in Sources */,
 				ECD0AC232928F2A70091B6EB /* CPDFTextViewController.m in Sources */,
 				ECD0AC262928F2E50091B6EB /* CPDFImageViewController.m in Sources */,

+ 3 - 2
PDFViewer/Controller/CPDFTextViewController.h

@@ -6,7 +6,8 @@
 //
 
 #import <UIKit/UIKit.h>
-
+#import "CPDFTextPreview.h"
+#import "CPDFDrawView.h"
 
 @class CPDFTextPreview;
 @class CPDFTextView;
@@ -14,7 +15,7 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@interface CPDFTextViewController : UIViewController
+@interface CPDFTextViewController : UIViewController  <CPDFTextPreviewDelegate>
 
 @property (nonatomic,strong) CPDFTextPreview *textPreview;
 @property (nonatomic,strong) CPDFTextView *textView;

+ 44 - 33
PDFViewer/Controller/CPDFTextViewController.m

@@ -7,13 +7,12 @@
 
 #import "CPDFTextViewController.h"
 #import "CPDFTextView.h"
-#import "CPDFTextPreview.h"
 #import "CPDFDataModel.h"
 #import "Masonry.h"
 
-@interface CPDFTextViewController () <CPDFTextPreviewDelegate>
+@interface CPDFTextViewController () 
 
-@property (nonatomic,assign) CGContextRef context;
+@property (nonatomic,strong) CPDFDrawView *drawView;
 
 @end
 
@@ -107,8 +106,8 @@
 
 - (void)onTextScaleChanged:(UISlider *)sender {
     if (!_dataModel.isTile) {
-        sender.minimumValue = 24;
-        sender.maximumValue = 48;
+        sender.minimumValue = 12;
+        sender.maximumValue = 36;
         _textPreview.watermarkLabel.font = [_textPreview.watermarkLabel.font fontWithSize:sender.value];
         [_textPreview.watermarkLabel sizeToFit];
         [_dataModel setWatermarkScale:sender.value / 24];
@@ -117,16 +116,22 @@
 
 - (void)onTileSwitchChanged:(UISwitch *) sender {
     if ([sender isOn]) {
-        [self drawMytext:self.context];
         _dataModel.isTile = YES;
         _textView.horizontalField.enabled = YES;
         _textView.verticalField.enabled = YES;
         _textView.textScaleSlider.enabled = NO;
+        
+        _drawView = [[CPDFDrawView alloc] initWithFrame:self.textPreview.bounds];
+        _drawView.delegate = self;
+        [self.view addSubview:_drawView];
+        [self.textPreview bringSubviewToFront:_drawView];
+        
     } else {
         _dataModel.isTile = NO;
         _textView.horizontalField.enabled = NO;
         _textView.verticalField.enabled = NO;
         _textView.textScaleSlider.enabled = YES;
+        [_drawView removeFromSuperview];
     }
 }
 
@@ -163,32 +168,6 @@
     [self presentViewController:alertController animated:YES completion:nil];
 }
 
-- (void)drawMytext:(CGContextRef)context {
-    CGContextSetFillColorWithColor(context, self.textPreview.watermarkLabel.textColor.CGColor);
-    
-    UIFont *font = [UIFont systemFontOfSize:self.textView.textScaleSlider.value];
-    NSDictionary *dic = @{NSFontAttributeName:font,NSForegroundColorAttributeName:self.textPreview.watermarkLabel.textColor};
-    
-    NSInteger wx,wy,height,width;
-    wx = self.textPreview.watermarkLabel.frame.origin.x;
-    wy = self.textPreview.watermarkLabel.frame.origin.y;
-    height = self.textPreview.watermarkLabel.frame.size.height;
-    width = self.textPreview.watermarkLabel.frame.size.width;
-    
-    NSInteger x,y,a,b;
-    x = wx / width + (self.textPreview.frame.size.width - (wx + width)) / width + 1;
-    y = wy / height + (self.textPreview.frame.size.height - (wy + height)) / height + 1;
-
-    a = wx % width;
-    b = wy % height;
-
-    for (NSInteger i = 0; i < x; i++) {
-        for (NSInteger j = 0; j < y; j++) {
-            
-            [@"watermark" drawInRect:CGRectMake(a + i * (width + 50), b + j * (height + 50), width, height) withAttributes:dic];
-        }
-    }
-}
 
 #pragma mark - Gesture
 
@@ -278,7 +257,39 @@
 #pragma mark - CPDFTextPreviewDelegate
 
 - (void)drawText:(CGContextRef)context {
-    _context = context;
+    CGContextSetFillColorWithColor(context, self.textPreview.watermarkLabel.textColor.CGColor);
+    CGContextSaveGState(context);
+    
+    CGContextClipToRect(context, self.textPreview.documentView.frame);
+    
+    UIFont *font = [UIFont systemFontOfSize:self.textView.textScaleSlider.value];
+    NSDictionary *dic = @{NSFontAttributeName:font,NSForegroundColorAttributeName:self.textPreview.watermarkLabel.textColor};
+    
+    self.drawView.transform = CGAffineTransformMakeRotation((self.dataModel.watermarkRotation * M_PI) / 180);
+    
+    NSInteger wx,wy,height,width;
+    wx = self.textPreview.watermarkLabel.frame.origin.x + self.textPreview.documentView.frame.origin.x;
+    wy = self.textPreview.watermarkLabel.frame.origin.y + self.textPreview.documentView.frame.origin.y;
+    height = self.textPreview.watermarkLabel.frame.size.height + 10;
+    width = self.textPreview.watermarkLabel.frame.size.width + 10;
+
+    NSInteger x,y,a,b;
+    x = wx / width + (self.textPreview.frame.size.width - (wx + width)) / width + 1;
+    y = wy / height + (self.textPreview.frame.size.height - (wy + height)) / height + 1;
+
+    a = wx % width;
+    b = wy % height;
+
+    for (NSInteger i = 0; i < x; i++) {
+        for (NSInteger j = 0; j < y; j++) {
+            [@"watermark" drawInRect:CGRectMake(a + i * width, b + j * height, width - 10, height - 10) withAttributes:dic];
+        }
+    }
+    
+    CGContextRestoreGState(context);
+    
+    //self.drawView.transform = CGAffineTransformMakeRotation((self.dataModel.watermarkRotation * M_PI) / 180);
+    
 }
 
 @end

+ 26 - 0
PDFViewer/View/CPDFDrawView.h

@@ -0,0 +1,26 @@
+//
+//  CPDFDrawView.h
+//  PDFViewer
+//
+//  Created by kdanmobile_2 on 2022/12/22.
+//
+
+#import <UIKit/UIKit.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@protocol CPDFTextPreviewDelegate <NSObject>
+
+@required
+
+- (void)drawText:(CGContextRef)context;
+
+@end
+
+@interface CPDFDrawView : UIView
+
+@property (nonatomic,strong) id<CPDFTextPreviewDelegate> delegate;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 30 - 0
PDFViewer/View/CPDFDrawView.m

@@ -0,0 +1,30 @@
+//
+//  CPDFDrawView.m
+//  PDFViewer
+//
+//  Created by kdanmobile_2 on 2022/12/22.
+//
+
+#import "CPDFDrawView.h"
+
+@implementation CPDFDrawView
+
+- (instancetype)initWithFrame:(CGRect)frame {
+    self = [super initWithFrame:frame];
+    
+    if (self) {
+        self.backgroundColor = [UIColor clearColor];
+    }
+    
+    return self;
+}
+
+- (void)drawRect:(CGRect)rect {
+    [super drawRect:rect];
+    
+    CGContextRef context = UIGraphicsGetCurrentContext();
+    
+    [self.delegate drawText:context];
+}
+
+@end

+ 0 - 8
PDFViewer/View/CPDFTextPreview.h

@@ -9,20 +9,12 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@protocol CPDFTextPreviewDelegate <NSObject>
-
-- (void)drawText:(CGContextRef)context;
-
-@end
-
 @interface CPDFTextPreview : UIView 
 
 @property (nonatomic,strong) UILabel *watermarkLabel;
 @property (nonatomic,strong) UIButton *rotationBtn;
 @property (nonatomic,strong) UIImageView *documentView;
 
-@property (nonatomic,strong) id<CPDFTextPreviewDelegate> delegate;
-
 @end
 
 NS_ASSUME_NONNULL_END

+ 2 - 4
PDFViewer/View/CPDFTextPreview.m

@@ -22,6 +22,8 @@ BOOL gTextPreviewIsFirstLayout = YES;
         [self addSubview:_documentView];
         [_documentView addSubview:_watermarkLabel];
         [_watermarkLabel addSubview:_rotationBtn];
+        
+        self.backgroundColor = [UIColor systemGray5Color];
     }
     return self;
 }
@@ -30,10 +32,6 @@ BOOL gTextPreviewIsFirstLayout = YES;
 
 - (void)drawRect:(CGRect)rect {
     [super drawRect:rect];
-    
-    CGContextRef context = UIGraphicsGetCurrentContext();
-    
-    [self.delegate drawText:context];
 
     _watermarkLabel.layer.borderWidth = 1;
     _watermarkLabel.layer.borderColor = UIColor.blueColor.CGColor;