CPDFNoteOpenViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // CPDFNoteOpenViewController.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. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFNoteOpenViewController.h"
  13. #import <ComPDFKit/ComPDFKit.h>
  14. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  15. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  16. #else
  17. #import "ComPDFKit_Tools.h"
  18. #endif
  19. #pragma mark - UIPopBackgroundView
  20. @interface UIPopBackgroundView : UIPopoverBackgroundView
  21. @property (nonatomic, assign) CGFloat fArrowOffset;
  22. @property (nonatomic, assign) UIPopoverArrowDirection direction;
  23. @end
  24. @implementation UIPopBackgroundView
  25. + (BOOL)wantsDefaultContentAppearance {
  26. return NO;
  27. }
  28. + (CGFloat)arrowBase{
  29. return 0;
  30. }
  31. + (UIEdgeInsets)contentViewInsets {
  32. return UIEdgeInsetsMake(0, 0, 0, 0);
  33. }
  34. + (CGFloat)arrowHeight{
  35. return 0;
  36. }
  37. - (UIPopoverArrowDirection)arrowDirection {
  38. return self.direction;
  39. }
  40. - (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {
  41. self.direction = arrowDirection;
  42. }
  43. - (void)setArrowOffset:(CGFloat)arrowOffset {
  44. self.fArrowOffset = arrowOffset;
  45. }
  46. - (instancetype)initWithFrame:(CGRect)frame {
  47. self = [super initWithFrame:frame];
  48. if (self) {
  49. self.backgroundColor = [UIColor clearColor];
  50. self.layer.cornerRadius = 3.0f;
  51. UIView *shadowView = [[UIView alloc] initWithFrame:frame];
  52. shadowView.backgroundColor = self.backgroundColor;
  53. [self addSubview:shadowView];
  54. shadowView.layer.shadowColor = [UIColor colorWithRed:163.0/255.0 green:163.0/255.0 blue:163.0/255.0 alpha:0.5].CGColor;
  55. shadowView.layer.shadowOpacity = 1.0f;
  56. shadowView.layer.shadowRadius = 1.0;
  57. shadowView.layer.shadowOffset = CGSizeMake(2, 1);
  58. shadowView.layer.cornerRadius = 3.0f;
  59. CALayer *maskLayer = [CALayer layer];
  60. maskLayer.frame = shadowView.layer.bounds;
  61. maskLayer.masksToBounds = YES;
  62. [shadowView.layer addSublayer:maskLayer];
  63. }
  64. return self;
  65. }
  66. - (void)layoutSubviews {
  67. [super layoutSubviews];
  68. }
  69. @end
  70. #pragma mark - CPDFNoteOpenViewController
  71. @interface CPDFNoteOpenViewController () <UIPopoverPresentationControllerDelegate, UITextViewDelegate>
  72. @property (nonatomic, strong) UITextView *noteTextView;
  73. @property (nonatomic, strong) NSString *textViewContent;
  74. @property (nonatomic, strong) CPDFAnnotation * annotation;
  75. @property (nonatomic, strong) UIView *contentView;
  76. @end
  77. @implementation CPDFNoteOpenViewController
  78. - (instancetype)initWithAnnotation:(id)annotation {
  79. if(self = [super init]) {
  80. self.annotation = annotation;
  81. }
  82. return self;
  83. }
  84. #pragma mark - UIViewController Methods
  85. - (void)viewDidLoad {
  86. [super viewDidLoad];
  87. // Do any additional setup after loading the view.
  88. self.view.backgroundColor = [CPDFColorUtils CNoteOpenBackgooundColor];
  89. _noteTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height-45)];
  90. _noteTextView.delegate = self;
  91. _noteTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  92. [_noteTextView setBackgroundColor:[UIColor clearColor]];
  93. [_noteTextView setFont:[UIFont systemFontOfSize:14]];
  94. [_noteTextView setTextAlignment:NSTextAlignmentLeft];
  95. [_noteTextView setTextColor:[UIColor blackColor]];
  96. [self.view addSubview:_noteTextView];
  97. _noteTextView.text = self.textViewContent;
  98. UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  99. [deleteButton setImage:[UIImage imageNamed:@"CPDFNoteContentImageNameDelete"
  100. inBundle:[NSBundle bundleForClass:self.class]
  101. compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  102. [deleteButton sizeToFit];
  103. CGRect frame = deleteButton.frame;
  104. frame.origin.x = 10;
  105. frame.origin.y = self.view.bounds.size.height-deleteButton.bounds.size.height-10;
  106. deleteButton.frame = frame;
  107. deleteButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
  108. [deleteButton addTarget:self action:@selector(buttonItemClicked_Delete:) forControlEvents:UIControlEventTouchUpInside];
  109. [self.view addSubview:deleteButton];
  110. UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
  111. [saveButton setImage:[UIImage imageNamed:@"CPDFNoteContentImageNameSave"
  112. inBundle:[NSBundle bundleForClass:self.class]
  113. compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  114. [saveButton sizeToFit];
  115. frame = saveButton.frame;
  116. frame.origin.x = self.view.bounds.size.width-saveButton.bounds.size.width-10;
  117. frame.origin.y = self.view.bounds.size.height-saveButton.bounds.size.height-10;
  118. saveButton.frame = frame;
  119. saveButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
  120. [saveButton addTarget:self action:@selector(buttonItemClicked_Save:) forControlEvents:UIControlEventTouchUpInside];
  121. [self.view addSubview:saveButton];
  122. self.noteTextView.text = self.annotation.contents?:@"";
  123. }
  124. - (void)viewDidAppear:(BOOL)animation {
  125. [super viewDidAppear:animation];
  126. [_noteTextView becomeFirstResponder];
  127. }
  128. - (void)viewWillDisappear:(BOOL)animation {
  129. [super viewWillDisappear:animation];
  130. if ([_noteTextView isFirstResponder]) {
  131. [_noteTextView resignFirstResponder];
  132. }
  133. }
  134. - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect {
  135. self.preferredContentSize = CGSizeMake(280, 305);
  136. self.modalPresentationStyle = UIModalPresentationPopover;
  137. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  138. popVC.delegate = self;
  139. popVC.sourceRect = rect;
  140. popVC.sourceView = viewController.view;
  141. popVC.canOverlapSourceViewRect = YES;
  142. popVC.popoverBackgroundViewClass = [UIPopBackgroundView class];
  143. [viewController presentViewController:self animated:YES completion:nil];
  144. }
  145. #pragma mark - Button Event Action
  146. - (void)buttonItemClicked_Delete:(id)sender {
  147. [self dismissViewControllerAnimated:YES completion:^{
  148. if (self.delegate && [self.delegate respondsToSelector:@selector(getNoteOpenViewController:content:isDelete:)]) {
  149. [self.delegate getNoteOpenViewController:self content:self.noteTextView.text isDelete:YES];
  150. }
  151. }];
  152. }
  153. - (void)buttonItemClicked_Save:(id)sender {
  154. [self dismissViewControllerAnimated:YES completion:^{
  155. if (self.delegate && [self.delegate respondsToSelector:@selector(getNoteOpenViewController:content:isDelete:)]) {
  156. [self.delegate getNoteOpenViewController:self content:self.noteTextView.text isDelete:NO];
  157. }
  158. }];
  159. }
  160. #pragma mark - UIPopoverPresentationControllerDelegate
  161. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  162. return UIModalPresentationNone;
  163. }
  164. - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
  165. if (self.delegate && [self.delegate respondsToSelector:@selector(getNoteOpenViewController:content:isDelete:)]) {
  166. [self.delegate getNoteOpenViewController:self content:self.noteTextView.text isDelete:NO];
  167. }
  168. }
  169. @end