CPDFNoteViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // CPDFNoteViewController.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 "CPDFNoteViewController.h"
  13. #import "CPDFSampleView.h"
  14. #import "CPDFColorSelectView.h"
  15. #import "CPDFColorPickerView.h"
  16. #import "CAnnotStyle.h"
  17. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  18. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  19. #else
  20. #import "ComPDFKit_Tools.h"
  21. #endif
  22. @interface CPDFNoteViewController () <UIColorPickerViewControllerDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate>
  23. @property (nonatomic, strong) CPDFSampleView *sampleView;
  24. @property (nonatomic, strong) UIView *sampleBackgoundView;
  25. @property (nonatomic, strong) CPDFColorSelectView *colorView;
  26. @property (nonatomic, strong) CPDFColorPickerView *colorPicker;
  27. @property (nonatomic, strong) UILabel *titleLabel;
  28. @property (nonatomic, strong) CAnnotStyle *annotStyle;
  29. @property (nonatomic, strong) UIButton *backBtn;
  30. @end
  31. @implementation CPDFNoteViewController
  32. #pragma mark - Initializers
  33. - (instancetype)initWithStyle:(CAnnotStyle *)annotStyle {
  34. if (self = [super init]) {
  35. self.annotStyle = annotStyle;
  36. }
  37. return self;
  38. }
  39. #pragma mark - ViewController Methods
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. self.titleLabel = [[UILabel alloc] init];
  44. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  45. self.titleLabel.text = NSLocalizedString(@"Note", nil);
  46. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  47. self.titleLabel.font = [UIFont systemFontOfSize:20];
  48. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  49. [self.view addSubview:self.titleLabel];
  50. self.backBtn = [[UIButton alloc] init];
  51. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
  52. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  53. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  54. [self.view addSubview:self.backBtn];
  55. self.sampleBackgoundView = [[UIView alloc] init];
  56. self.sampleBackgoundView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  57. self.sampleBackgoundView.layer.borderWidth = 1.0;
  58. self.sampleBackgoundView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  59. [self.view addSubview:self.sampleBackgoundView];
  60. self.sampleView = [[CPDFSampleView alloc] init];
  61. self.sampleView.backgroundColor = [UIColor whiteColor];
  62. self.sampleView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  63. self.sampleView.layer.borderWidth = 1.0;
  64. self.sampleView.opcity = 1.0;
  65. self.sampleView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  66. [self.sampleBackgoundView addSubview:self.sampleView];
  67. self.colorView = [[CPDFColorSelectView alloc] init];
  68. self.colorView.selectedColor = self.annotStyle.color;
  69. self.colorView.delegate = self;
  70. self.colorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  71. [self.view addSubview:self.colorView];
  72. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  73. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  74. }
  75. - (void)viewWillLayoutSubviews {
  76. [super viewWillLayoutSubviews];
  77. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, ((self.view.frame.size.height)/7));
  78. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, ((self.view.frame.size.height)/7));
  79. self.sampleBackgoundView.frame = CGRectMake(0, ((self.view.frame.size.height)/7), self.view.bounds.size.width, ((self.view.frame.size.height)/7)*3);
  80. self.sampleView.frame = CGRectMake((self.view.frame.size.width - 300)/2, 15, 300, self.sampleBackgoundView.bounds.size.height - 30);
  81. self.colorView.frame = CGRectMake(0, ((self.view.frame.size.height)/7)*4, self.view.frame.size.width, ((self.view.frame.size.height)/7)*2);
  82. if (@available(iOS 11.0, *)) {
  83. self.colorPicker.frame = CGRectMake(self.view.safeAreaInsets.left, 0, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height);
  84. self.colorView.frame = CGRectMake(self.view.safeAreaInsets.left, ((self.view.frame.size.height)/7)*4,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, ((self.view.frame.size.height)/7)*2);
  85. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, ((self.view.frame.size.height)/7));
  86. } else {
  87. self.colorPicker.frame = self.view.frame;
  88. self.colorView.frame = CGRectMake(0, ((self.view.frame.size.height)/7)*4, self.view.frame.size.width, ((self.view.frame.size.height)/7)*2);
  89. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, ((self.view.frame.size.height)/7));
  90. }
  91. }
  92. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  93. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  94. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  95. }
  96. - (void)viewWillAppear:(BOOL)animated {
  97. self.sampleView.color = self.annotStyle.color;
  98. self.sampleView.selecIndex = CPDFSamplesNote;
  99. [self.sampleView setNeedsDisplay];
  100. }
  101. #pragma mark - Protect Methods
  102. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  103. {
  104. self.preferredContentSize = CGSizeMake(MIN(self.view.bounds.size.width, self.view.bounds.size.height), traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 320 : 320);
  105. }
  106. #pragma mark - Action
  107. - (void)buttonItemClicked_back:(id)sender {
  108. [self dismissViewControllerAnimated:YES completion:nil];
  109. }
  110. #pragma mark - CPDFColorSelectViewDelegate
  111. - (void)selectColorView:(CPDFColorSelectView *)select {
  112. if (@available(iOS 14.0, *)) {
  113. UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
  114. picker.delegate = self;
  115. [self presentViewController:picker animated:YES completion:nil];
  116. } else {
  117. _colorPicker = [[CPDFColorPickerView alloc] initWithFrame:self.view.frame];
  118. _colorPicker.delegate = self;
  119. _colorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  120. _colorPicker.backgroundColor = [UIColor whiteColor];
  121. [self.view addSubview:self.colorPicker];
  122. }
  123. }
  124. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  125. self.sampleView.color = color;
  126. self.annotStyle.color = color;
  127. [self.sampleView setNeedsDisplay];
  128. if (self.delegate && [self.delegate respondsToSelector:@selector(noteViewController:annotSytle:)]) {
  129. [self.delegate noteViewController:self annotSytle:self.annotStyle];
  130. }
  131. }
  132. #pragma mark - CPDFColorPickerViewDelegate
  133. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  134. self.sampleView.color = color;
  135. self.annotStyle.color = color;
  136. [self.sampleView setNeedsDisplay];
  137. if (self.delegate && [self.delegate respondsToSelector:@selector(noteViewController:annotSytle:)]) {
  138. [self.delegate noteViewController:self annotSytle:self.annotStyle];
  139. }
  140. }
  141. #pragma mark - UIColorPickerViewControllerDelegate
  142. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  143. self.sampleView.color = viewController.selectedColor;
  144. self.annotStyle.color = self.sampleView.color;
  145. if (self.delegate && [self.delegate respondsToSelector:@selector(noteViewController:annotSytle:)]) {
  146. [self.delegate noteViewController:self annotSytle:self.annotStyle];
  147. }
  148. [self.sampleView setNeedsDisplay];
  149. }
  150. @end