// // PDFNoteViewController.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFNoteViewController.h" @interface PDFNoteViewController () { UITextView *_noteTextView; } @property (nonatomic,retain) NSString *textViewContent; @end @implementation PDFNoteViewController #pragma mark - Init Mehods - (void)dealloc { Block_release(_callback); [_textViewContent release]; [_noteTextView setDelegate:nil]; [_noteTextView release]; [super dealloc]; } #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor colorWithRed:253.0/255.0 green:246.0/255.0 blue:214.0/255.0 alpha:1.0]]; _noteTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-35)]; _noteTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; [_noteTextView setBackgroundColor:[UIColor clearColor]]; [_noteTextView setFont:[UIFont systemFontOfSize:14]]; [_noteTextView setTextAlignment:NSTextAlignmentLeft]; [_noteTextView setTextColor:[UIColor blackColor]]; [self.view addSubview:_noteTextView]; _noteTextView.text = self.textViewContent; UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeSystem]; [deleteButton setTitle:NSLocalizedString(@"Delete", nil) forState:UIControlStateNormal]; [deleteButton sizeToFit]; CGRect frame = deleteButton.frame; frame.origin.x = 10; frame.origin.y = self.view.bounds.size.height-deleteButton.bounds.size.height-10; deleteButton.frame = frame; deleteButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; [deleteButton addTarget:self action:@selector(buttonItemClicked_Delete:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:deleteButton]; UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem]; [saveButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal]; [saveButton sizeToFit]; frame = saveButton.frame; frame.origin.x = self.view.bounds.size.width-saveButton.bounds.size.width-10; frame.origin.y = self.view.bounds.size.height-saveButton.bounds.size.height-10; saveButton.frame = frame; saveButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin; [saveButton addTarget:self action:@selector(buttonItemClicked_Save:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:saveButton]; } - (void)viewDidAppear:(BOOL)animation { [super viewDidAppear:animation]; [_noteTextView becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animation { [super viewWillDisappear:animation]; if ([_noteTextView isFirstResponder]) { [_noteTextView resignFirstResponder]; } } #pragma mark - Public Methods - (void)setContent:(NSString *)content { if (!content) { self.textViewContent = @""; } else { self.textViewContent = content; } } - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect { self.preferredContentSize = CGSizeMake(300, 240); self.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popVC = self.popoverPresentationController; popVC.delegate = self; popVC.sourceRect = rect; popVC.sourceView = viewController.view; [viewController presentViewController:self animated:YES completion:nil]; } #pragma mark - Button Event Action - (void)buttonItemClicked_Delete:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ if (self.callback) { self.callback(@"", YES); self.callback = nil; } }]; } - (void)buttonItemClicked_Save:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ if (self.callback) { self.callback(_noteTextView.text, NO); self.callback = nil; } }]; } #pragma mark - UIPopoverPresentationControllerDelegate - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController { if (self.callback) { self.callback(_noteTextView.text, NO); self.callback = nil; } } @end