CPDFEditToolBar.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // CPDFEditToolBar.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. //
  11. #import "CPDFEditToolBar.h"
  12. #import <ComPDFKit/ComPDFKit.h>
  13. #import "CPDFColorUtils.h"
  14. @interface CPDFEditToolBar()
  15. @property(nonatomic,strong) UIButton * undoButton;
  16. @property(nonatomic,strong) UIButton * redoButton;
  17. @property(nonatomic,strong) UIButton * propertyButton;
  18. @property(nonatomic,strong) UIButton * textEditButton;
  19. @property(nonatomic,strong) UIButton * imageEditButton;
  20. @property(nonatomic,strong) UIView * leftView;
  21. @property(nonatomic,strong) UIView * rightView;
  22. @property(nonatomic,strong) UIView * splitView;
  23. @property(nonatomic, assign) CPDFEditMode editToolBarSelectType;
  24. @end
  25. @implementation CPDFEditToolBar
  26. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  27. if (self = [super init]) {
  28. _pdfView = pdfView;
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageChangedNotification:) name:CPDFViewPageChangedNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)layoutSubviews {
  34. [super layoutSubviews];
  35. [self removeViews];
  36. [self setUp];
  37. }
  38. - (void)removeViews{
  39. [self.leftView removeFromSuperview];
  40. [self.rightView removeFromSuperview];
  41. [self.splitView removeFromSuperview];
  42. }
  43. - (void)setUp {
  44. self.textEditButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
  45. [self.textEditButton addTarget:self action:@selector(textEditAction:) forControlEvents:UIControlEventTouchUpInside];
  46. [self.textEditButton setImage:[UIImage imageNamed:@"CPDFEditAddText" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  47. self.imageEditButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.textEditButton.frame) + 10, 7, 30, 30)];
  48. [self.imageEditButton addTarget:self action:@selector(imageEditAction:) forControlEvents:UIControlEventTouchUpInside];
  49. [self.imageEditButton setImage:[UIImage imageNamed:@"CPDFEditAddImage" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  50. self.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.bounds.size.width - 110, 44)];
  51. [self addSubview:self.leftView];
  52. [self.leftView addSubview:self.textEditButton];
  53. [self.leftView addSubview:self.imageEditButton];
  54. self.leftView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  55. self.textEditButton.frame = CGRectMake(CGRectGetMidX(self.leftView.frame) - 50, self.textEditButton.frame.origin.y, self.textEditButton.frame.size.width, self.textEditButton.frame.size.height);
  56. self.imageEditButton.frame = CGRectMake(CGRectGetMidX(self.leftView.frame) + 20, self.imageEditButton.frame.origin.y, self.imageEditButton.frame.size.width, self.imageEditButton.frame.size.height);
  57. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(19, 12, 1, 20)];
  58. if (@available(iOS 13.0, *)){
  59. if([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark)
  60. lineView.backgroundColor = [UIColor whiteColor];
  61. else
  62. lineView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
  63. } else
  64. lineView.backgroundColor = [UIColor blackColor];
  65. self.propertyButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 7, 30, 30)];
  66. [self.propertyButton setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageProperties" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  67. self.propertyButton.frame = CGRectMake(20, 7, 30, 30);
  68. [self.propertyButton addTarget:self action:@selector(propertyAction:) forControlEvents:UIControlEventTouchUpInside];
  69. self.undoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.propertyButton.frame), 7, 30, 30)];
  70. [self.undoButton addTarget:self action:@selector(undoAction:) forControlEvents:UIControlEventTouchUpInside];
  71. [self.undoButton setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageUndo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  72. self.redoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.undoButton.frame), 7,30, 30)];
  73. [self.redoButton addTarget:self action:@selector(redoAction:) forControlEvents:UIControlEventTouchUpInside];
  74. [self.redoButton setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageRedo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  75. self.rightView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.leftView.frame), 0, 110, 44)];
  76. [self addSubview:self.self.rightView];
  77. [self.rightView addSubview:self.redoButton];
  78. [self.rightView addSubview:self.undoButton];
  79. [self.rightView addSubview:self.propertyButton];
  80. [self.rightView addSubview:lineView];
  81. self.backgroundColor = [UIColor colorWithRed:0.98 green:0.99 blue:1.0 alpha:1.0];
  82. [self updateButtonState];
  83. self.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  84. self.editToolBarSelectType = CPDFEditModeAll;
  85. }
  86. #pragma mark - Action
  87. - (void)textEditAction:(UIButton*)sender {
  88. self.textEditButton.selected = !self.textEditButton.selected;
  89. if(self.textEditButton.selected == YES){
  90. [self.imageEditButton setSelected:NO];
  91. [self.imageEditButton setBackgroundColor:[UIColor clearColor]];
  92. }
  93. if(sender.selected == NO && self.imageEditButton.selected == NO){
  94. [self.pdfView changeEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  95. [self.pdfView setShouAddEditAreaType:CAddEditingAreaTypeNone];
  96. if(self.delegate && [self.delegate respondsToSelector:@selector(editClickInToolBar:editMode:)]){
  97. [self.delegate editClickInToolBar:self editMode:CPDFEditModeAll];
  98. }
  99. self.editToolBarSelectType = CPDFEditModeAll;
  100. }else{
  101. [self.pdfView changeEditingLoadType:CEditingLoadTypeText];
  102. [self.pdfView setShouAddEditAreaType:CAddEditingAreaTypeText];
  103. if(self.delegate && [self.delegate respondsToSelector:@selector(editClickInToolBar:editMode:)]){
  104. [self.delegate editClickInToolBar:self editMode:CPDFEditModeText];
  105. }
  106. self.editToolBarSelectType = CPDFEditModeText;
  107. }
  108. [self updateButtonState];
  109. if(sender.selected == YES){
  110. [self.textEditButton setBackgroundColor:[UIColor colorWithRed:221./255. green:233/255. blue:255./255 alpha:1.]];
  111. }else{
  112. [self.textEditButton setBackgroundColor:[UIColor clearColor]];
  113. }
  114. }
  115. - (void)imageEditAction:(UIButton*)sender {
  116. self.imageEditButton.selected = !self.imageEditButton.selected;
  117. if(self.imageEditButton.selected == YES){
  118. [self.textEditButton setSelected:NO];
  119. [self.textEditButton setBackgroundColor:[UIColor clearColor]];
  120. }
  121. if(sender.selected == NO && self.textEditButton.selected == NO){
  122. [self.pdfView changeEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  123. [self.pdfView setShouAddEditAreaType:CAddEditingAreaTypeNone];
  124. if(self.delegate && [self.delegate respondsToSelector:@selector(editClickInToolBar:editMode:)]){
  125. [self.delegate editClickInToolBar:self editMode:CPDFEditModeAll];
  126. }
  127. self.editToolBarSelectType = CPDFEditModeAll;
  128. } else{
  129. [self.pdfView changeEditingLoadType:CEditingLoadTypeImage];
  130. [self.pdfView setShouAddEditAreaType:CAddEditingAreaTypeImage];
  131. if(self.delegate && [self.delegate respondsToSelector:@selector(editClickInToolBar:editMode:)]){
  132. [self.delegate editClickInToolBar:self editMode:CPDFEditModeImage];
  133. }
  134. self.editToolBarSelectType = CPDFEditModeImage;
  135. }
  136. [self updateButtonState];
  137. if(sender.selected == YES){
  138. [self.imageEditButton setBackgroundColor:[UIColor colorWithRed:221./255. green:233/255. blue:255./255 alpha:1.]];
  139. }else{
  140. [self.imageEditButton setBackgroundColor:[UIColor clearColor]];
  141. }
  142. }
  143. - (void)redoAction:(UIButton*)sender {
  144. if(self.delegate && [self.delegate respondsToSelector:@selector(redoDidClickInToolBar:)]){
  145. [self.delegate redoDidClickInToolBar:self];
  146. }
  147. }
  148. - (void)undoAction:(UIButton*)sender {
  149. if(self.delegate && [self.delegate respondsToSelector:@selector(undoDidClickInToolBar:)]){
  150. [self.delegate undoDidClickInToolBar:self];
  151. }
  152. }
  153. - (void)propertyAction:(UIButton*)sender {
  154. if(self.delegate && [self.delegate respondsToSelector:@selector(propertyEditDidClickInToolBar:)]){
  155. [self.delegate propertyEditDidClickInToolBar:self];
  156. }
  157. }
  158. - (void)updateButtonState {
  159. if (self.pdfView.editingLoadType == CEditingLoadTypeText) {
  160. //Text
  161. [self.textEditButton setSelected:YES];
  162. [self.imageEditButton setSelected:NO];
  163. } else if((self.pdfView.editingLoadType == CEditingLoadTypeImage)){
  164. [self.textEditButton setSelected:NO];
  165. [self.imageEditButton setSelected:YES];
  166. } else {
  167. [self.textEditButton setSelected:NO];
  168. [self.imageEditButton setSelected:NO];
  169. }
  170. if(self.textEditButton.selected == YES){
  171. [self.textEditButton setBackgroundColor:[UIColor colorWithRed:221./255. green:233/255. blue:255./255 alpha:1.]];
  172. }else{
  173. [self.textEditButton setBackgroundColor:[UIColor clearColor]];
  174. }
  175. if(self.imageEditButton.selected == YES){
  176. [self.imageEditButton setBackgroundColor:[UIColor colorWithRed:221./255. green:233/255. blue:255./255 alpha:1.]];
  177. }else{
  178. [self.imageEditButton setBackgroundColor:[UIColor clearColor]];
  179. }
  180. if (CAddEditingAreaTypeText == self.pdfView.shouAddEditAreaType) {
  181. self.propertyButton.enabled = YES;
  182. } else if (self.pdfView.editStatus == CEditingSelectStateEmpty){
  183. self.propertyButton.enabled = NO;
  184. } else{
  185. self.propertyButton.enabled = YES;
  186. }
  187. if ([self.pdfView canEditTextRedo])
  188. self.redoButton.enabled = YES;
  189. else
  190. self.redoButton.enabled = NO;
  191. if ([self.pdfView canEditTextUndo])
  192. self.undoButton.enabled = YES;
  193. else
  194. self.undoButton.enabled = NO;
  195. }
  196. #pragma mark - NSNotification
  197. - (void)pageChangedNotification:(NSNotification *)notification {
  198. CPDFView *pdfview = notification.object;
  199. if (pdfview.document == self.pdfView.document) {
  200. [self updateButtonState];
  201. }
  202. }
  203. @end