PDFBottomToolbar.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // PDFBottomToolbar.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 "PDFBottomToolbar.h"
  11. #import "PDFViewerModeViewController.h"
  12. @interface PDFBottomToolbar ()
  13. @property (nonatomic,retain) UILabel *pageLabel;
  14. @property (nonatomic,retain) UIButton *bookmarkButton;
  15. @end
  16. @implementation PDFBottomToolbar
  17. #pragma mark - Init Methods
  18. - (id)initWithFrame:(CGRect)frame {
  19. if (self = [super initWithFrame:frame]) {
  20. self.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
  21. self.layer.borderColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0].CGColor;
  22. self.layer.borderWidth = 1.0;
  23. UIButton *pervButton = [UIButton buttonWithType:UIButtonTypeCustom];
  24. [pervButton setImage:[UIImage imageNamed:@"btn_frontpage.png"] forState:UIControlStateNormal];
  25. [pervButton addTarget:self action:@selector(buttonItemClicked_Previous:) forControlEvents:UIControlEventTouchUpInside];
  26. pervButton.frame = CGRectMake(0, 0, 50, frame.size.height);
  27. [self addSubview:pervButton];
  28. UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  29. [nextButton setImage:[UIImage imageNamed:@"btn_nextpage.png"] forState:UIControlStateNormal];
  30. [nextButton addTarget:self action:@selector(buttonItemClicked_Next:) forControlEvents:UIControlEventTouchUpInside];
  31. nextButton.frame = CGRectMake(frame.size.width-50, 0, 50, frame.size.height);
  32. [self addSubview:nextButton];
  33. UIButton *viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
  34. [viewButton setImage:[UIImage imageNamed:@"btn_viewer_mode00.png"]forState:UIControlStateNormal];
  35. [viewButton addTarget:self action:@selector(buttonItemClicked_ViewerMode:) forControlEvents:UIControlEventTouchUpInside];
  36. viewButton.frame = CGRectMake(50, 0, 50, frame.size.height);
  37. [self addSubview:viewButton];
  38. _bookmarkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  39. [_bookmarkButton setImage:[UIImage imageNamed:@"btn_bookmark01.png"] forState:UIControlStateNormal];
  40. [_bookmarkButton setImage:[UIImage imageNamed:@"btn_bookmark02.png"] forState:UIControlStateSelected];
  41. [_bookmarkButton addTarget:self action:@selector(buttonItemClicked_Bookmark:) forControlEvents:UIControlEventTouchUpInside];
  42. _bookmarkButton.frame = CGRectMake(frame.size.width-100, 0, 50, frame.size.height);
  43. [self addSubview:_bookmarkButton];
  44. _pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100, frame.size.height)];
  45. _pageLabel.font = [UIFont systemFontOfSize:16];
  46. [_pageLabel setBackgroundColor:[UIColor clearColor]];
  47. _pageLabel.textAlignment = NSTextAlignmentCenter;
  48. _pageLabel.textColor = [UIColor colorWithRed:106.0/255.0 green:106.0/255.0 blue:106.0/255.0 alpha:1.0];
  49. _pageLabel.text = @"0 / 0";
  50. [self addSubview:_pageLabel];
  51. }
  52. return self;
  53. }
  54. - (void)dealloc {
  55. [_bookmarkButton release];
  56. [_pageLabel release];
  57. [super dealloc];
  58. }
  59. #pragma mark - Public Methods
  60. - (void)reloadData {
  61. NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex];
  62. NSInteger pageCount = [self.pdfViewController.pdfView.document pageCount];
  63. self.pageLabel.text = [NSString stringWithFormat:@"%@ / %@",@(pageIndex+1),@(pageCount)];
  64. self.bookmarkButton.selected = [self.pdfViewController.pdfView.document bookmarkForPageIndex:pageIndex] ? YES : NO;
  65. }
  66. #pragma mark - Button Actions
  67. - (void)buttonItemClicked_Previous:(UIButton *)button {
  68. NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex];
  69. if (pageIndex <= 0) {
  70. pageIndex = 0;
  71. } else {
  72. pageIndex--;
  73. }
  74. [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:YES];
  75. }
  76. - (void)buttonItemClicked_Next:(UIButton *)button {
  77. NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex];
  78. NSInteger pageCount = [self.pdfViewController.pdfView.document pageCount];
  79. if (pageIndex >= pageCount-1) {
  80. pageIndex = pageCount-1;
  81. } else {
  82. pageIndex++;
  83. }
  84. [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:YES];
  85. }
  86. - (void)buttonItemClicked_ViewerMode:(UIButton *)button {
  87. __block PDFViewerModeViewController *vc = [[PDFViewerModeViewController alloc] init];
  88. vc.themesType = self.pdfViewController.pdfView.displayMode;
  89. vc.color = self.pdfViewController.pdfView.displayModeCustomColor;
  90. vc.brightness = [UIScreen mainScreen].brightness;
  91. vc.scrollType = self.pdfViewController.pdfView.displayDirection;
  92. vc.isCropMode = self.pdfViewController.pdfView.displayCrop;
  93. vc.callback = ^(NSInteger index) {
  94. if (index == 0) {
  95. self.pdfViewController.pdfView.displayModeCustomColor = vc.color;
  96. self.pdfViewController.pdfView.displayMode = vc.themesType;
  97. [self.pdfViewController.pdfView layoutDocumentView];
  98. } else if (index == 1) {
  99. [UIScreen mainScreen].brightness = vc.brightness;
  100. } else if (index == 2) {
  101. self.pdfViewController.pdfView.displayDirection = vc.scrollType;
  102. [self.pdfViewController.pdfView layoutDocumentView];
  103. } else if (index == 3) {
  104. self.pdfViewController.view.window.userInteractionEnabled = NO;
  105. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  106. self.pdfViewController.pdfView.displayCrop = vc.isCropMode;
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. self.pdfViewController.view.window.userInteractionEnabled = YES;
  109. [self.pdfViewController.pdfView layoutDocumentView];
  110. });
  111. });
  112. }
  113. };
  114. CGRect rect = [button.superview convertRect:button.frame toView:self.pdfViewController.view];
  115. [vc showViewController:self.pdfViewController inRect:rect];
  116. [vc release];
  117. }
  118. - (void)buttonItemClicked_Bookmark:(UIButton *)button {
  119. NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex];
  120. if ([self.pdfViewController.pdfView.document bookmarkForPageIndex:pageIndex]) {
  121. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil)
  122. style:UIAlertActionStyleDefault
  123. handler:^(UIAlertAction *action){
  124. [self.pdfViewController.pdfView.document removeBookmarkForPageIndex:pageIndex];
  125. [self.pdfViewController.pdfView setNeedsDisplayForVisiblePages];
  126. self.bookmarkButton.selected = NO;
  127. }];
  128. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil)
  129. style:UIAlertActionStyleCancel
  130. handler:nil];
  131. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attention", nil)
  132. message:NSLocalizedString(@"Do you want to remove old bookmark?", nil)
  133. preferredStyle:UIAlertControllerStyleAlert];
  134. [alert addAction:okAction];
  135. [alert addAction:cancelAction];
  136. [self.pdfViewController presentViewController:alert animated:YES completion:nil];
  137. } else {
  138. NSString *text = [NSString stringWithFormat:NSLocalizedString(@"Page %ld", nil), pageIndex+1];
  139. [self.pdfViewController.pdfView.document addBookmark:text forPageIndex:pageIndex];
  140. [self.pdfViewController.pdfView setNeedsDisplayForVisiblePages];
  141. self.bookmarkButton.selected = YES;
  142. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  143. message:NSLocalizedString(@"Bookmark added successfully.", nil)
  144. preferredStyle:UIAlertControllerStyleAlert];
  145. [self.pdfViewController presentViewController:alert animated:YES completion:nil];
  146. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  147. [alert dismissViewControllerAnimated:YES completion:nil];
  148. });
  149. }
  150. }
  151. @end