PDFToolbar.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // PDFToolbar.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFToolbar.h"
  11. #import <ComPDFKit/ComPDFKit.h>
  12. #import "PDFColorViewController.h"
  13. #import "PDFShapeViewController.h"
  14. #import "PDFFreehandViewController.h"
  15. @interface PDFBarButton : UIButton
  16. @property (nonatomic,assign) NSInteger type;
  17. @property (nonatomic,retain) UIColor *lineColor;
  18. @end
  19. @implementation PDFBarButton
  20. - (void)dealloc {
  21. [_lineColor release];
  22. [super dealloc];
  23. }
  24. - (void)drawRect:(CGRect)rect {
  25. [super drawRect:rect];
  26. CGRect imageFrame = self.imageView.frame;
  27. CGContextRef ctx = UIGraphicsGetCurrentContext();
  28. CGContextSetStrokeColorWithColor(ctx, self.lineColor.CGColor);
  29. if (self.type == 1) {
  30. CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame)-2, CGRectGetMidY(imageFrame));
  31. CGContextSetLineWidth(ctx, CGRectGetHeight(imageFrame));
  32. CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame)+2, CGRectGetMidY(imageFrame));
  33. }else if (self.type == 2) {
  34. CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMaxY(imageFrame));
  35. CGContextSetLineWidth(ctx, 2.0);
  36. CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame), CGRectGetMaxY(imageFrame));
  37. } else if (self.type == 3) {
  38. CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMidY(imageFrame));
  39. CGContextSetLineWidth(ctx, 2.0);
  40. CGContextAddLineToPoint(ctx, CGRectGetMaxX(imageFrame), CGRectGetMidY(imageFrame));
  41. } else if (self.type == 4) {
  42. float tWidth = imageFrame.size.width / 6.0;
  43. CGContextMoveToPoint(ctx, CGRectGetMinX(imageFrame), CGRectGetMaxY(imageFrame));
  44. CGContextSetLineWidth(ctx, 2.0);
  45. CGContextAddCurveToPoint(ctx,
  46. CGRectGetMinX(imageFrame)+tWidth,CGRectGetMaxY(imageFrame)+4,
  47. CGRectGetMinX(imageFrame)+tWidth*2.0,CGRectGetMaxY(imageFrame)-4,
  48. CGRectGetMinX(imageFrame)+tWidth*3.0,CGRectGetMaxY(imageFrame));
  49. CGContextAddCurveToPoint(ctx,
  50. CGRectGetMinX(imageFrame)+tWidth*4.0,CGRectGetMaxY(imageFrame)+4,
  51. CGRectGetMinX(imageFrame)+tWidth*5.0,CGRectGetMaxY(imageFrame)-4,
  52. CGRectGetMinX(imageFrame)+tWidth*6.0,CGRectGetMaxY(imageFrame));
  53. }
  54. CGContextStrokePath(ctx);
  55. }
  56. @end
  57. @interface PDFToolbar ()
  58. @property (nonatomic,retain) UIScrollView *scrollView;
  59. @property (nonatomic,retain) NSArray *buttons;
  60. @property (nonatomic,assign) NSInteger selectedIndex;
  61. @end
  62. @implementation PDFToolbar
  63. #pragma mark - Init Methods
  64. - (void)dealloc {
  65. Block_release(_callback);
  66. [_buttons release];
  67. [_scrollView release];
  68. [super dealloc];
  69. }
  70. - (id)initWithFrame:(CGRect)frame {
  71. if (self = [super initWithFrame:frame]) {
  72. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  73. UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
  74. UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
  75. effectView.frame = self.bounds;
  76. effectView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  77. [self addSubview:effectView];
  78. UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)] autorelease];
  79. line.backgroundColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0];
  80. line.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  81. [self addSubview:line];
  82. } else {
  83. self.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
  84. self.layer.borderColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0].CGColor;
  85. self.layer.borderWidth = 1.0;
  86. }
  87. self.selectedIndex = -1;
  88. [self initSubview];
  89. }
  90. return self;
  91. }
  92. - (void)initSubview {
  93. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  94. _scrollView.showsVerticalScrollIndicator = NO;
  95. _scrollView.showsHorizontalScrollIndicator = NO;
  96. [self addSubview:_scrollView];
  97. CGFloat tOffsetX = 0.0;
  98. NSArray *images = @[@"btn_note01.png",
  99. @"btn_t1.png",
  100. @"btn_t1.png",
  101. @"btn_t1.png",
  102. @"btn_t1.png",
  103. @"btn_freehand1.png",
  104. @"btn_shape.png",
  105. @"btn_typewrite.png",
  106. @"btn_sign1.png",
  107. @"btn_stamp_01.png",
  108. @"btn_photo01.png",
  109. @"btn_link_01.png"];
  110. NSMutableArray *buttons = [NSMutableArray array];
  111. for (int i=0; i<images.count; i++) {
  112. PDFBarButton *button = [PDFBarButton buttonWithType:UIButtonTypeCustom];
  113. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  114. button.frame = CGRectMake(tOffsetX, 1, 58, 43);
  115. } else {
  116. button.frame = CGRectMake(2, tOffsetX, 40, 50);
  117. }
  118. [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
  119. button.tag = i;
  120. button.type = i;
  121. if (i == 1) {
  122. button.lineColor = CPDFKitShareConfig.highlightAnnotationColor;
  123. } else if (i == 2) {
  124. button.lineColor = CPDFKitShareConfig.underlineAnnotationColor;
  125. } else if (i == 3) {
  126. button.lineColor = CPDFKitShareConfig.strikeoutAnnotationColor;
  127. } else if (i == 4) {
  128. button.lineColor = CPDFKitShareConfig.squigglyAnnotationColor;
  129. } else if (i == 5) {
  130. button.type = 4;
  131. button.lineColor = CPDFKitShareConfig.freehandAnnotationColor;
  132. }
  133. [button addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  134. [_scrollView addSubview:button];
  135. [buttons addObject:button];
  136. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  137. tOffsetX += button.bounds.size.width;
  138. } else {
  139. tOffsetX += button.bounds.size.height;
  140. }
  141. UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc]
  142. initWithTarget:self
  143. action:@selector(buttonGestureRecognizer_LongPress:)];
  144. longpressGesture.minimumPressDuration = 0.6;
  145. longpressGesture.allowableMovement = 15;
  146. [button addGestureRecognizer:longpressGesture];
  147. [longpressGesture release];
  148. }
  149. self.buttons = buttons;
  150. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  151. _scrollView.contentSize = CGSizeMake(tOffsetX, _scrollView.bounds.size.height);
  152. } else {
  153. _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, tOffsetX);
  154. }
  155. }
  156. - (void)layoutSubviews {
  157. [super layoutSubviews];
  158. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  159. if (self.scrollView.contentSize.width > self.bounds.size.width) {
  160. self.scrollView.frame = self.bounds;
  161. } else {
  162. CGRect frame = self.scrollView.frame;
  163. frame.origin.x = (self.bounds.size.width-self.scrollView.contentSize.width)/2.0;
  164. frame.size.width = self.scrollView.contentSize.width;
  165. self.scrollView.frame = frame;
  166. }
  167. }
  168. }
  169. #pragma mark - Public Methods
  170. - (void)reloadData {
  171. if (self.selectedIndex >=0 &&
  172. self.selectedIndex < self.buttons.count) {
  173. UIButton *selectedButton = [self.buttons objectAtIndex:self.selectedIndex];
  174. selectedButton.backgroundColor = [UIColor clearColor];
  175. }
  176. self.selectedIndex = -1;
  177. }
  178. #pragma mark - Button Event Action
  179. - (void)buttonItemClicked_Switch:(UIButton *)button {
  180. if (self.selectedIndex >=0 &&
  181. self.selectedIndex < self.buttons.count) {
  182. UIButton *selectedButton = [self.buttons objectAtIndex:self.selectedIndex];
  183. selectedButton.backgroundColor = [UIColor clearColor];
  184. }
  185. if (button.tag != self.selectedIndex) {
  186. button.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:239.0/255.0 blue:253.0/255.0 alpha:1.0];
  187. self.selectedIndex = button.tag;
  188. if (self.callback) {
  189. self.callback(self.selectedIndex, YES, button);
  190. }
  191. } else {
  192. if (self.callback) {
  193. self.callback(self.selectedIndex, NO, button);
  194. }
  195. self.selectedIndex = -1;
  196. }
  197. }
  198. - (void)buttonGestureRecognizer_LongPress:(UILongPressGestureRecognizer *)recognizer {
  199. if (recognizer.state != UIGestureRecognizerStateBegan) {
  200. return;
  201. }
  202. PDFBarButton *button = (PDFBarButton *)recognizer.view;
  203. NSInteger index = button.tag;
  204. if (index >= 1 && index <= 4) {
  205. __block PDFColorViewController *vc = [[PDFColorViewController alloc] init];
  206. if (index == 1) {
  207. vc.color = CPDFKitShareConfig.highlightAnnotationColor;
  208. } else if (index == 2) {
  209. vc.color = CPDFKitShareConfig.underlineAnnotationColor;
  210. } else if (index == 3) {
  211. vc.color = CPDFKitShareConfig.strikeoutAnnotationColor;
  212. } else if (index == 4) {
  213. vc.color = CPDFKitShareConfig.squigglyAnnotationColor;
  214. }
  215. vc.opacity = CPDFKitShareConfig.markupAnnotationOpacity*100;
  216. vc.callback = ^{
  217. button.lineColor = vc.color;
  218. [button setNeedsDisplay];
  219. if (index == 1) {
  220. CPDFKitShareConfig.highlightAnnotationColor = vc.color;
  221. } else if (index == 2) {
  222. CPDFKitShareConfig.underlineAnnotationColor = vc.color;
  223. } else if (index == 3) {
  224. CPDFKitShareConfig.strikeoutAnnotationColor = vc.color;
  225. } else if (index == 4) {
  226. CPDFKitShareConfig.squigglyAnnotationColor = vc.color;
  227. }
  228. CPDFKitShareConfig.markupAnnotationOpacity = vc.opacity/100;
  229. };
  230. UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  231. CGRect rect = [button.superview convertRect:button.frame toView:viewController.view];
  232. [vc showViewController:viewController inRect:rect];
  233. [vc release];
  234. } else if (index == 5) {
  235. __block PDFFreehandViewController *vc = [[PDFFreehandViewController alloc] init];
  236. vc.color = CPDFKitShareConfig.freehandAnnotationColor;
  237. vc.opacity = CPDFKitShareConfig.freehandAnnotationOpacity;
  238. vc.borderWidth = CPDFKitShareConfig.freehandAnnotationBorderWidth;
  239. vc.callback = ^{
  240. button.lineColor = vc.color;
  241. [button setNeedsDisplay];
  242. CPDFKitShareConfig.freehandAnnotationColor = vc.color;
  243. CPDFKitShareConfig.freehandAnnotationOpacity = vc.opacity;
  244. CPDFKitShareConfig.freehandAnnotationBorderWidth = vc.borderWidth;
  245. if (button.tag == self.selectedIndex) {
  246. if (self.callback) {
  247. self.callback(self.selectedIndex, YES, button);
  248. }
  249. }
  250. };
  251. UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  252. CGRect rect = [button.superview convertRect:button.frame toView:viewController.view];
  253. [vc showViewController:viewController inRect:rect];
  254. [vc release];
  255. } else if (index == 6) {
  256. __block PDFShapeViewController *vc = [[PDFShapeViewController alloc] init];
  257. vc.allowsShowStyle = YES;
  258. vc.color = CPDFKitShareConfig.shapeAnnotationColor;
  259. vc.fillColor = CPDFKitShareConfig.shapeAnnotationInteriorColor;
  260. vc.opacity = CPDFKitShareConfig.shapeAnnotationOpacity*100;
  261. vc.fillOpacity = CPDFKitShareConfig.shapeAnnotationInteriorOpacity*100;
  262. vc.borderWidth = CPDFKitShareConfig.shapeAnnotationBorderWidth;
  263. vc.style = self.shapeStyle;
  264. vc.callback = ^{
  265. CPDFKitShareConfig.shapeAnnotationColor = vc.color;
  266. CPDFKitShareConfig.shapeAnnotationInteriorColor = vc.fillColor;
  267. CPDFKitShareConfig.shapeAnnotationOpacity = vc.opacity/100;
  268. CPDFKitShareConfig.shapeAnnotationInteriorOpacity = vc.fillOpacity/100;
  269. CPDFKitShareConfig.shapeAnnotationBorderWidth = vc.borderWidth;
  270. self.shapeStyle = vc.style;
  271. if (button.tag == self.selectedIndex) {
  272. if (self.callback) {
  273. self.callback(self.selectedIndex, YES, button);
  274. }
  275. }
  276. };
  277. UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  278. CGRect rect = [button.superview convertRect:button.frame toView:viewController.view];
  279. [vc showViewController:viewController inRect:rect];
  280. [vc release];
  281. }
  282. }
  283. @end