123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- //
- // 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 <ComPDFKit/ComPDFKit.h>
- #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<images.count; i++) {
- PDFBarButton *button = [PDFBarButton buttonWithType:UIButtonTypeCustom];
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- button.frame = CGRectMake(tOffsetX, 1, 58, 43);
- } else {
- button.frame = CGRectMake(2, tOffsetX, 40, 50);
- }
- [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
- button.tag = i;
- button.type = i;
- if (i == 1) {
- button.lineColor = CPDFKitShareConfig.highlightAnnotationColor;
- } else if (i == 2) {
- button.lineColor = CPDFKitShareConfig.underlineAnnotationColor;
- } else if (i == 3) {
- button.lineColor = CPDFKitShareConfig.strikeoutAnnotationColor;
- } else if (i == 4) {
- button.lineColor = CPDFKitShareConfig.squigglyAnnotationColor;
- } else if (i == 5) {
- button.type = 4;
- button.lineColor = CPDFKitShareConfig.freehandAnnotationColor;
- }
- [button addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
- [_scrollView addSubview:button];
- [buttons addObject:button];
-
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- tOffsetX += button.bounds.size.width;
- } else {
- tOffsetX += button.bounds.size.height;
- }
-
- UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc]
- initWithTarget:self
- action:@selector(buttonGestureRecognizer_LongPress:)];
- longpressGesture.minimumPressDuration = 0.6;
- longpressGesture.allowableMovement = 15;
- [button addGestureRecognizer:longpressGesture];
- [longpressGesture release];
- }
- self.buttons = buttons;
-
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- _scrollView.contentSize = CGSizeMake(tOffsetX, _scrollView.bounds.size.height);
- } else {
- _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, tOffsetX);
- }
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
-
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- if (self.scrollView.contentSize.width > 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
|