PDFNoteViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // PDFNoteViewController.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 "PDFNoteViewController.h"
  11. @interface PDFNoteViewController () <UIPopoverPresentationControllerDelegate> {
  12. UITextView *_noteTextView;
  13. }
  14. @property (nonatomic,retain) NSString *textViewContent;
  15. @end
  16. @implementation PDFNoteViewController
  17. #pragma mark - Init Mehods
  18. - (void)dealloc {
  19. Block_release(_callback);
  20. [_textViewContent release];
  21. [_noteTextView setDelegate:nil];
  22. [_noteTextView release];
  23. [super dealloc];
  24. }
  25. #pragma mark - UIViewController Methods
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self.view setBackgroundColor:[UIColor colorWithRed:253.0/255.0 green:246.0/255.0 blue:214.0/255.0 alpha:1.0]];
  29. _noteTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-35)];
  30. _noteTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  31. [_noteTextView setBackgroundColor:[UIColor clearColor]];
  32. [_noteTextView setFont:[UIFont systemFontOfSize:14]];
  33. [_noteTextView setTextAlignment:NSTextAlignmentLeft];
  34. [_noteTextView setTextColor:[UIColor blackColor]];
  35. [self.view addSubview:_noteTextView];
  36. _noteTextView.text = self.textViewContent;
  37. UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
  38. [deleteButton setTitle:NSLocalizedString(@"Delete", nil) forState:UIControlStateNormal];
  39. [deleteButton sizeToFit];
  40. CGRect frame = deleteButton.frame;
  41. frame.origin.x = 10;
  42. frame.origin.y = self.view.bounds.size.height-deleteButton.bounds.size.height-10;
  43. deleteButton.frame = frame;
  44. deleteButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
  45. [deleteButton addTarget:self action:@selector(buttonItemClicked_Delete:) forControlEvents:UIControlEventTouchUpInside];
  46. [self.view addSubview:deleteButton];
  47. UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
  48. [saveButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
  49. [saveButton sizeToFit];
  50. frame = saveButton.frame;
  51. frame.origin.x = self.view.bounds.size.width-saveButton.bounds.size.width-10;
  52. frame.origin.y = self.view.bounds.size.height-saveButton.bounds.size.height-10;
  53. saveButton.frame = frame;
  54. saveButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
  55. [saveButton addTarget:self action:@selector(buttonItemClicked_Save:) forControlEvents:UIControlEventTouchUpInside];
  56. [self.view addSubview:saveButton];
  57. }
  58. - (void)viewDidAppear:(BOOL)animation {
  59. [super viewDidAppear:animation];
  60. [_noteTextView becomeFirstResponder];
  61. }
  62. - (void)viewWillDisappear:(BOOL)animation {
  63. [super viewWillDisappear:animation];
  64. if ([_noteTextView isFirstResponder]) {
  65. [_noteTextView resignFirstResponder];
  66. }
  67. }
  68. #pragma mark - Public Methods
  69. - (void)setContent:(NSString *)content {
  70. if (!content) {
  71. self.textViewContent = @"";
  72. } else {
  73. self.textViewContent = content;
  74. }
  75. }
  76. - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect {
  77. self.preferredContentSize = CGSizeMake(300, 240);
  78. self.modalPresentationStyle = UIModalPresentationPopover;
  79. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  80. popVC.delegate = self;
  81. popVC.sourceRect = rect;
  82. popVC.sourceView = viewController.view;
  83. [viewController presentViewController:self animated:YES completion:nil];
  84. }
  85. #pragma mark - Button Event Action
  86. - (void)buttonItemClicked_Delete:(id)sender {
  87. [self dismissViewControllerAnimated:YES completion:^{
  88. if (self.callback) {
  89. self.callback(@"", YES);
  90. self.callback = nil;
  91. }
  92. }];
  93. }
  94. - (void)buttonItemClicked_Save:(id)sender {
  95. [self dismissViewControllerAnimated:YES completion:^{
  96. if (self.callback) {
  97. self.callback(_noteTextView.text, NO);
  98. self.callback = nil;
  99. }
  100. }];
  101. }
  102. #pragma mark - UIPopoverPresentationControllerDelegate
  103. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  104. return UIModalPresentationNone;
  105. }
  106. - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
  107. if (self.callback) {
  108. self.callback(_noteTextView.text, NO);
  109. self.callback = nil;
  110. }
  111. }
  112. @end