// // PDFToolbar.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFToolbar.h" #import #import "PDFColorViewController.h" #import "PDFShapeViewController.h" #import "PDFFreehandViewController.h" @interface PDFBarButton : UIButton @property (nonatomic,assign) NSInteger type; @property (nonatomic,retain) UIColor *lineColor; @end @implementation PDFBarButton - (void)dealloc { [_lineColor release]; [super dealloc]; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGRect imageFrame = self.imageView.frame; CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(ctx, self.lineColor.CGColor); if (self.type == 1) { CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame)-2, CGRectGetMidY(imageFrame)); CGContextSetLineWidth(ctx, CGRectGetHeight(imageFrame)); CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame)+2, CGRectGetMidY(imageFrame)); }else if (self.type == 2) { CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMaxY(imageFrame)); CGContextSetLineWidth(ctx, 2.0); CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame), CGRectGetMaxY(imageFrame)); } else if (self.type == 3) { CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMidY(imageFrame)); CGContextSetLineWidth(ctx, 2.0); CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame), CGRectGetMidY(imageFrame)); } else if (self.type == 4) { float tWidth = imageFrame.size.width / 6.0; CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMaxY(imageFrame)); CGContextSetLineWidth(ctx, 2.0); CGContextAddCurveToPoint(ctx, CGRectGetMinX(imageFrame)+tWidth,CGRectGetMaxY(imageFrame)+4, CGRectGetMinX(imageFrame)+tWidth*2.0,CGRectGetMaxY(imageFrame)-4, CGRectGetMinX(imageFrame)+tWidth*3.0,CGRectGetMaxY(imageFrame)); CGContextAddCurveToPoint(ctx, CGRectGetMinX(imageFrame)+tWidth*4.0,CGRectGetMaxY(imageFrame)+4, CGRectGetMinX(imageFrame)+tWidth*5.0,CGRectGetMaxY(imageFrame)-4, CGRectGetMinX(imageFrame)+tWidth*6.0,CGRectGetMaxY(imageFrame)); } CGContextStrokePath(ctx); } @end @interface PDFToolbar () @property (nonatomic,retain) UIScrollView *scrollView; @property (nonatomic,retain) NSArray *buttons; @property (nonatomic,assign) NSInteger selectedIndex; @end @implementation PDFToolbar #pragma mark - Init Methods - (void)dealloc { Block_release(_callback); [_buttons release]; [_scrollView release]; [super dealloc]; } - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) { UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur]; effectView.frame = self.bounds; effectView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; [self addSubview:effectView]; UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)] autorelease]; line.backgroundColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0]; line.autoresizingMask = UIViewAutoresizingFlexibleWidth; [self addSubview:line]; } else { self.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0]; self.layer.borderColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0].CGColor; self.layer.borderWidth = 1.0; } self.selectedIndex = -1; [self initSubview]; } return self; } - (void)initSubview { _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; [self addSubview:_scrollView]; CGFloat tOffsetX = 0.0; NSArray *images = @[@"btn_note01.png", @"btn_t1.png", @"btn_t1.png", @"btn_t1.png", @"btn_t1.png", @"btn_freehand1.png", @"btn_shape.png", @"btn_typewrite.png", @"btn_sign1.png", @"btn_stamp_01.png", @"btn_photo01.png", @"btn_link_01.png"]; NSMutableArray *buttons = [NSMutableArray array]; for (int i=0; i self.bounds.size.width) { self.scrollView.frame = self.bounds; } else { CGRect frame = self.scrollView.frame; frame.origin.x = (self.bounds.size.width-self.scrollView.contentSize.width)/2.0; frame.size.width = self.scrollView.contentSize.width; self.scrollView.frame = frame; } } } #pragma mark - Public Methods - (void)reloadData { if (self.selectedIndex >=0 && self.selectedIndex < self.buttons.count) { UIButton *selectedButton = [self.buttons objectAtIndex:self.selectedIndex]; selectedButton.backgroundColor = [UIColor clearColor]; } self.selectedIndex = -1; } #pragma mark - Button Event Action - (void)buttonItemClicked_Switch:(UIButton *)button { if (self.selectedIndex >=0 && self.selectedIndex < self.buttons.count) { UIButton *selectedButton = [self.buttons objectAtIndex:self.selectedIndex]; selectedButton.backgroundColor = [UIColor clearColor]; } if (button.tag != self.selectedIndex) { button.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:239.0/255.0 blue:253.0/255.0 alpha:1.0]; self.selectedIndex = button.tag; if (self.callback) { self.callback(self.selectedIndex, YES, button); } } else { if (self.callback) { self.callback(self.selectedIndex, NO, button); } self.selectedIndex = -1; } } - (void)buttonGestureRecognizer_LongPress:(UILongPressGestureRecognizer *)recognizer { if (recognizer.state != UIGestureRecognizerStateBegan) { return; } PDFBarButton *button = (PDFBarButton *)recognizer.view; NSInteger index = button.tag; if (index >= 1 && index <= 4) { __block PDFColorViewController *vc = [[PDFColorViewController alloc] init]; if (index == 1) { vc.color = CPDFKitShareConfig.highlightAnnotationColor; } else if (index == 2) { vc.color = CPDFKitShareConfig.underlineAnnotationColor; } else if (index == 3) { vc.color = CPDFKitShareConfig.strikeoutAnnotationColor; } else if (index == 4) { vc.color = CPDFKitShareConfig.squigglyAnnotationColor; } vc.opacity = CPDFKitShareConfig.markupAnnotationOpacity*100; vc.callback = ^{ button.lineColor = vc.color; [button setNeedsDisplay]; if (index == 1) { CPDFKitShareConfig.highlightAnnotationColor = vc.color; } else if (index == 2) { CPDFKitShareConfig.underlineAnnotationColor = vc.color; } else if (index == 3) { CPDFKitShareConfig.strikeoutAnnotationColor = vc.color; } else if (index == 4) { CPDFKitShareConfig.squigglyAnnotationColor = vc.color; } CPDFKitShareConfig.markupAnnotationOpacity = vc.opacity/100; }; UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController; CGRect rect = [button.superview convertRect:button.frame toView:viewController.view]; [vc showViewController:viewController inRect:rect]; [vc release]; } else if (index == 5) { __block PDFFreehandViewController *vc = [[PDFFreehandViewController alloc] init]; vc.color = CPDFKitShareConfig.freehandAnnotationColor; vc.opacity = CPDFKitShareConfig.freehandAnnotationOpacity; vc.borderWidth = CPDFKitShareConfig.freehandAnnotationBorderWidth; vc.callback = ^{ button.lineColor = vc.color; [button setNeedsDisplay]; CPDFKitShareConfig.freehandAnnotationColor = vc.color; CPDFKitShareConfig.freehandAnnotationOpacity = vc.opacity; CPDFKitShareConfig.freehandAnnotationBorderWidth = vc.borderWidth; if (button.tag == self.selectedIndex) { if (self.callback) { self.callback(self.selectedIndex, YES, button); } } }; UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController; CGRect rect = [button.superview convertRect:button.frame toView:viewController.view]; [vc showViewController:viewController inRect:rect]; [vc release]; } else if (index == 6) { __block PDFShapeViewController *vc = [[PDFShapeViewController alloc] init]; vc.allowsShowStyle = YES; vc.color = CPDFKitShareConfig.shapeAnnotationColor; vc.fillColor = CPDFKitShareConfig.shapeAnnotationInteriorColor; vc.opacity = CPDFKitShareConfig.shapeAnnotationOpacity*100; vc.fillOpacity = CPDFKitShareConfig.shapeAnnotationInteriorOpacity*100; vc.borderWidth = CPDFKitShareConfig.shapeAnnotationBorderWidth; vc.style = self.shapeStyle; vc.callback = ^{ CPDFKitShareConfig.shapeAnnotationColor = vc.color; CPDFKitShareConfig.shapeAnnotationInteriorColor = vc.fillColor; CPDFKitShareConfig.shapeAnnotationOpacity = vc.opacity/100; CPDFKitShareConfig.shapeAnnotationInteriorOpacity = vc.fillOpacity/100; CPDFKitShareConfig.shapeAnnotationBorderWidth = vc.borderWidth; self.shapeStyle = vc.style; if (button.tag == self.selectedIndex) { if (self.callback) { self.callback(self.selectedIndex, YES, button); } } }; UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController; CGRect rect = [button.superview convertRect:button.frame toView:viewController.view]; [vc showViewController:viewController inRect:rect]; [vc release]; } } @end