Browse Source

PDFView(ios) - 水印12/23

yangliuhua 2 years ago
parent
commit
09e7da2de7

+ 6 - 0
PDFViewer.xcodeproj/project.pbxproj

@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
 		C96150D2295400B00020F445 /* CPDFDrawView.m in Sources */ = {isa = PBXBuildFile; fileRef = C96150D1295400B00020F445 /* CPDFDrawView.m */; };
+		C96150D529554F660020F445 /* CPDFClipView.m in Sources */ = {isa = PBXBuildFile; fileRef = C96150D429554F660020F445 /* CPDFClipView.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 */; };
@@ -57,6 +58,8 @@
 /* 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>"; };
+		C96150D329554F660020F445 /* CPDFClipView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFClipView.h; sourceTree = "<group>"; };
+		C96150D429554F660020F445 /* CPDFClipView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFClipView.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>"; };
@@ -223,6 +226,8 @@
 				ECD0ABEA2928ED990091B6EB /* CPDFTextView.m */,
 				C96150D0295400B00020F445 /* CPDFDrawView.h */,
 				C96150D1295400B00020F445 /* CPDFDrawView.m */,
+				C96150D329554F660020F445 /* CPDFClipView.h */,
+				C96150D429554F660020F445 /* CPDFClipView.m */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -351,6 +356,7 @@
 				ECD0AC1D2928EECB0091B6EB /* MASViewAttribute.m in Sources */,
 				ECD0AC182928EECB0091B6EB /* MASCompositeConstraint.m in Sources */,
 				ECD0AC1C2928EECB0091B6EB /* View+MASAdditions.m in Sources */,
+				C96150D529554F660020F445 /* CPDFClipView.m in Sources */,
 				ECD0AC152928EECB0091B6EB /* MASLayoutConstraint.m in Sources */,
 				EC62978E29220F8300D339EE /* AppDelegate.m in Sources */,
 				EC62979F29220F8400D339EE /* main.m in Sources */,

+ 0 - 2
PDFViewer/Controller/CPDFAddViewController.m

@@ -93,7 +93,6 @@
         
         [_textViewController.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.right.equalTo(self.view.mas_right).offset(-38);
-//            make.left.equalTo(_textViewController.textPreview.mas_right).offset(0);
             make.height.equalTo(@205);
             make.width.equalTo(@393);
             make.bottom.equalTo(self.view.mas_bottom).offset(-74);
@@ -107,7 +106,6 @@
         
         [_imageViewController.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.right.equalTo(self.view.mas_right).offset(-38);
-//            make.left.equalTo(_imageViewController.imagePreview.mas_right).offset(0);
             make.height.equalTo(@205);
             make.width.equalTo(@393);
             make.bottom.equalTo(self.view.mas_bottom).offset(-74);

+ 64 - 3
PDFViewer/Controller/CPDFImageViewController.m

@@ -9,12 +9,17 @@
 #import "CPDFImageView.h"
 #import "CPDFImagePreview.h"
 #import "CPDFDataModel.h"
+#import "CPDFDrawView.h"
+#import "CPDFClipView.h"
 #import "Masonry.h"
 
-@interface CPDFImageViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
+@interface CPDFImageViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,CPDFTextPreviewDelegate,CPDFClipTextPreviewDelegate>
 
 @property (nonatomic,assign) CGRect watermarkFrame;
 
+@property (nonatomic,strong) CPDFDrawView *drawView;
+@property (nonatomic,strong) CPDFClipView *cliView;
+
 @end
 
 @implementation CPDFImageViewController
@@ -168,8 +173,11 @@
     sender.minimumValue = 1;
     sender.maximumValue = 3;
     
-    _watermarkFrame.size.width = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.width;
-    _watermarkFrame.size.height = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.height;;
+//    _watermarkFrame.size.width = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.width;
+//    _watermarkFrame.size.height = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.height;
+    _watermarkFrame.size.width = sender.value * 33;
+    _watermarkFrame.size.height = sender.value * 33;
+    
     _watermarkFrame.origin.x = _imagePreview.watermarkView.frame.origin.x;
     _watermarkFrame.origin.y = _imagePreview.watermarkView.frame.origin.y;
     _imagePreview.watermarkView.frame = _watermarkFrame;
@@ -223,11 +231,25 @@
         _imageView.horizontalField.enabled = YES;
         _imageView.verticalField.enabled = YES;
         _imageView.imageScaleSlider.enabled = NO;
+        
+        _drawView = [[CPDFDrawView alloc] initWithFrame:self.imagePreview.bounds];
+        _drawView.delegate = self;
+        [self.view addSubview:_drawView];
+        [self.imagePreview bringSubviewToFront:_drawView];
+        
+        _cliView = [[CPDFClipView alloc] initWithFrame:self.imagePreview.bounds];
+        _cliView.delegate = self;
+        [self.view addSubview:_cliView];
+        [self.drawView bringSubviewToFront:_cliView];
+        
     } else {
         _dataModel.isTile = NO;
         _imageView.horizontalField.enabled = NO;
         _imageView.verticalField.enabled = NO;
         _imageView.imageScaleSlider.enabled = YES;
+        
+        [self.drawView removeFromSuperview];
+        [self.cliView removeFromSuperview];
     }
 }
 
@@ -257,4 +279,43 @@
     return newImage;
 }
 
+#pragma mark - CPDFTextPreviewDelegate
+
+- (void)drawText:(CGContextRef)context {
+    
+    CGContextSaveGState(context);
+    
+    UIImage *image = self.imagePreview.watermarkView.image;
+    
+    self.drawView.transform = CGAffineTransformMakeRotation((self.dataModel.watermarkRotation * M_PI) / 180);
+    
+    NSInteger wx,wy,height,width;
+    wx = self.imagePreview.watermarkView.frame.origin.x + self.imagePreview.documentView.frame.origin.x;
+    wy = self.imagePreview.watermarkView.frame.origin.y + self.imagePreview.documentView.frame.origin.y;
+    height = self.imagePreview.watermarkView.frame.size.height + 10;
+    width = self.imagePreview.watermarkView.frame.size.width + 10;
+
+    NSInteger x,y,a,b;
+    x = wx / width + (self.imagePreview.frame.size.width - (wx + width)) / width + 1;
+    y = wy / height + (self.imagePreview.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++) {
+            [image drawInRect:CGRectMake(a + i * width, b + j * height, width - 10, height - 10)];
+        }
+    }
+}
+
+#pragma mark - CPDFClipTextPreviewDelegate
+
+- (void)clipText:(CGContextRef)context {
+    CGContextSetFillColorWithColor(context, [UIColor systemGray5Color].CGColor);
+    CGContextFillRect(context, self.imagePreview.bounds);
+    CGContextClearRect(context, self.imagePreview.documentView.frame);
+}
+
+
 @end

+ 1 - 2
PDFViewer/Controller/CPDFTextViewController.h

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

+ 64 - 5
PDFViewer/Controller/CPDFTextViewController.m

@@ -8,11 +8,14 @@
 #import "CPDFTextViewController.h"
 #import "CPDFTextView.h"
 #import "CPDFDataModel.h"
+#import "CPDFDrawView.h"
+#import "CPDFClipView.h"
 #import "Masonry.h"
 
-@interface CPDFTextViewController () 
+@interface CPDFTextViewController () <CPDFTextPreviewDelegate,CPDFClipTextPreviewDelegate,UITextFieldDelegate>
 
 @property (nonatomic,strong) CPDFDrawView *drawView;
+@property (nonatomic,strong) CPDFClipView *cliView;
 
 @end
 
@@ -24,6 +27,10 @@
     
     _textPreview = [[CPDFTextPreview alloc] init];
     _textView = [[CPDFTextView alloc] init];
+    
+    _textView.verticalField.delegate = self;
+    _textView.horizontalField.delegate = self;
+    
     _dataModel = [[CPDFDataModel alloc] init];
     _alertController = [UIAlertController alertControllerWithTitle:@"Watermark Content" message:nil preferredStyle:UIAlertControllerStyleAlert];
     
@@ -126,12 +133,18 @@
         [self.view addSubview:_drawView];
         [self.textPreview bringSubviewToFront:_drawView];
         
+        _cliView = [[CPDFClipView alloc] initWithFrame:self.textPreview.bounds];
+        _cliView.delegate = self;
+        [self.view addSubview:_cliView];
+        [self.drawView bringSubviewToFront:_cliView];
+        
     } else {
         _dataModel.isTile = NO;
         _textView.horizontalField.enabled = NO;
         _textView.verticalField.enabled = NO;
         _textView.textScaleSlider.enabled = YES;
         [_drawView removeFromSuperview];
+        [_cliView removeFromSuperview];
     }
 }
 
@@ -260,8 +273,6 @@
     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};
     
@@ -285,11 +296,59 @@
             [@"watermark" drawInRect:CGRectMake(a + i * width, b + j * height, width - 10, height - 10) withAttributes:dic];
         }
     }
+}
+
+#pragma mark - CPDFClipTextPreviewDelegate
+
+- (void)clipText:(CGContextRef)context {
+    CGContextSetFillColorWithColor(context, [UIColor systemGray5Color].CGColor);
+    CGContextFillRect(context, self.textPreview.bounds);
+    CGContextClearRect(context, self.textPreview.documentView.frame);
+}
+
+#pragma mark - UITextFieldDelegate
+
+- (void)textFieldDidBeginEditing:(UITextField *)textField {
+    
+    self.cliView.hidden = YES;
+    self.drawView.hidden = YES;
+    
+    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
+        
+    } else {
+        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
+            make.height.mas_equalTo(205);
+            make.width.equalTo(_textPreview.mas_width);
+            make.bottom.equalTo(self.view.mas_bottom).offset(-500);
+        }];
+        
+        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
+            self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y - 500);
+        } completion:nil];
+    }
+}
+
+- (BOOL)textFieldShouldReturn:(UITextField *)textField {
+    [textField resignFirstResponder];
     
-    CGContextRestoreGState(context);
+    self.cliView.hidden = NO;
+    self.drawView.hidden = NO;
     
-    //self.drawView.transform = CGAffineTransformMakeRotation((self.dataModel.watermarkRotation * M_PI) / 180);
+    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
+        
+    } else {
+        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
+            make.top.equalTo(_textPreview.mas_bottom).offset(0);
+            make.width.equalTo(_textPreview.mas_width);
+            make.bottom.equalTo(self.view.mas_bottom).offset(0);
+        }];
+        
+        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
+            self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y + 300);
+        } completion:nil];
+    }
     
+    return YES;
 }
 
 @end

+ 27 - 0
PDFViewer/View/CPDFClipView.h

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

+ 30 - 0
PDFViewer/View/CPDFClipView.m

@@ -0,0 +1,30 @@
+//
+//  CPDFClipView.m
+//  PDFViewer
+//
+//  Created by kdanmobile_2 on 2022/12/23.
+//
+
+#import "CPDFClipView.h"
+
+@implementation CPDFClipView
+
+- (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 clipText:context];
+}
+
+@end