CPDFLinkViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // CPDFLinkViewController.m
  3. // compdfkit-tools
  4. //
  5. // Copyright © 2014-2023 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 "CPDFLinkViewController.h"
  13. #import "CPDFColorUtils.h"
  14. #import "CAnnotStyle.h"
  15. @interface CPDFLinkViewController ()<UITextFieldDelegate>
  16. @property (nonatomic, strong) CAnnotStyle *annotStyle;
  17. @property (nonatomic, strong) UIScrollView *scrcollView;
  18. @property (nonatomic, strong) UIButton *backBtn;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  21. @property (nonatomic, strong) UITextField *pageTextField;
  22. @property (nonatomic, strong) UITextField *emailTextField;
  23. @property (nonatomic, strong) UITextField *urlTextField;
  24. @property (nonatomic, strong) UIButton *saveButton;
  25. @property (nonatomic, assign) CPDFLinkType linkType;
  26. @end
  27. @implementation CPDFLinkViewController
  28. #pragma mark - Initializers
  29. - (instancetype)initWithStyle:(CAnnotStyle *)annotStyle {
  30. if (self = [super init]) {
  31. self.annotStyle = annotStyle;
  32. }
  33. return self;
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  38. [self initWithView];
  39. if(self.annotStyle.isSelectAnnot) {
  40. self.linkType = CPDFLinkTypeLink;
  41. self.segmentedControl.selectedSegmentIndex = 0;
  42. } else {
  43. CPDFLinkAnnotation *link = self.annotStyle.annotations.firstObject;
  44. NSString *url = link.URL;
  45. CPDFDestination *destination = link.destination;
  46. if (url) {
  47. if ([url hasPrefix:@"mailto:"]) {
  48. self.emailTextField.text = [url substringFromIndex:7]?:@"";
  49. self.linkType = CPDFLinkTypeEmail;
  50. self.segmentedControl.selectedSegmentIndex = 2;
  51. } else {
  52. self.urlTextField.text = url?:@"";
  53. self.linkType = CPDFLinkTypeLink;
  54. self.segmentedControl.selectedSegmentIndex = 0;
  55. }
  56. } else if (destination) {
  57. self.linkType = CPDFLinkTypePage;
  58. self.pageTextField.text = [NSString stringWithFormat:@"%@", @(destination.pageIndex+1)];
  59. self.segmentedControl.selectedSegmentIndex = 1;
  60. } else {
  61. self.linkType = CPDFLinkTypeLink;
  62. self.segmentedControl.selectedSegmentIndex = 0;
  63. }
  64. }
  65. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  66. }
  67. -(void)viewDidAppear:(BOOL)animated {
  68. [super viewDidAppear:animated];
  69. UITextField *currentTextField = nil;
  70. switch (_linkType) {
  71. case CPDFLinkTypeLink:
  72. currentTextField = self.urlTextField;
  73. break;
  74. case CPDFLinkTypePage:
  75. currentTextField = self.pageTextField;
  76. break;
  77. default:
  78. case CPDFLinkTypeEmail:
  79. currentTextField = self.emailTextField;
  80. break;
  81. }
  82. [currentTextField becomeFirstResponder];
  83. }
  84. - (void)setLinkType:(CPDFLinkType)linkType {
  85. _linkType = linkType;
  86. UITextField *currentTextField = nil;
  87. switch (_linkType) {
  88. case CPDFLinkTypeLink:
  89. self.pageTextField.hidden = self.emailTextField.hidden = YES;
  90. self.urlTextField.hidden = NO;
  91. currentTextField = self.urlTextField;
  92. break;
  93. case CPDFLinkTypePage:
  94. self.urlTextField.hidden = self.emailTextField.hidden = YES;
  95. self.pageTextField.hidden = NO;
  96. currentTextField = self.pageTextField;
  97. break;
  98. default:
  99. case CPDFLinkTypeEmail:
  100. self.urlTextField.hidden = self.pageTextField.hidden = YES;
  101. self.emailTextField.hidden = NO;
  102. currentTextField = self.emailTextField;
  103. break;
  104. }
  105. [currentTextField becomeFirstResponder];
  106. if(currentTextField.text.length > 0) {
  107. self.saveButton.enabled = YES;
  108. self.saveButton.backgroundColor = [UIColor systemBlueColor];
  109. [self.saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  110. } else {
  111. self.saveButton.enabled = NO;
  112. self.saveButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
  113. [self.saveButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  114. }
  115. }
  116. - (void)viewWillLayoutSubviews {
  117. [super viewWillLayoutSubviews];
  118. if (@available(iOS 11.0, *)) {
  119. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  120. } else {
  121. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  122. }
  123. if (@available(iOS 11.0, *)) {
  124. _scrcollView.frame = CGRectMake(self.view.safeAreaInsets.left, 50, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height);
  125. } else {
  126. _scrcollView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
  127. }
  128. self.scrcollView.contentSize = CGSizeMake(_scrcollView.frame.size.width, self.scrcollView.contentSize.height);
  129. self.saveButton.frame = CGRectMake((self.scrcollView.frame.size.width - 120)/2, self.saveButton.frame.origin.y, 120, 32);
  130. }
  131. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  132. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  133. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  134. }
  135. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  136. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 600);
  137. [self.urlTextField resignFirstResponder];
  138. [self.emailTextField resignFirstResponder];
  139. [self.pageTextField resignFirstResponder];
  140. }
  141. #pragma mark - Private
  142. - (void)initWithView {
  143. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 17.5, self.view.frame.size.width, 25.0)];
  144. _titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  145. _titleLabel.text = NSLocalizedString(@"Link to", nil);
  146. _titleLabel.textAlignment = NSTextAlignmentCenter;
  147. _titleLabel.font = [UIFont systemFontOfSize:20];
  148. _titleLabel.adjustsFontSizeToFitWidth = YES;
  149. [self.view addSubview:_titleLabel];
  150. _scrcollView = [[UIScrollView alloc] init];
  151. _scrcollView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
  152. _scrcollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  153. _scrcollView.scrollEnabled = YES;
  154. [self.view addSubview:_scrcollView];
  155. self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  156. if (@available(iOS 11.0, *)) {
  157. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  158. } else {
  159. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  160. }
  161. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  162. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  163. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  164. [self.view addSubview:self.backBtn];
  165. CGFloat offstY = 10;
  166. _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[NSLocalizedString(@"URL", nil), NSLocalizedString(@"Page",nil),NSLocalizedString(@"Email",nil)]];
  167. _segmentedControl.frame = CGRectMake(30, offstY, self.scrcollView.frame.size.width - 30 *2, 32.0);
  168. _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  169. [_segmentedControl addTarget:self action:@selector(segmentedControlValueChanged_Mode:) forControlEvents:UIControlEventValueChanged];
  170. [self.scrcollView addSubview:_segmentedControl];
  171. offstY +=_segmentedControl.frame.size.height;
  172. offstY+= 32.0;
  173. _urlTextField = [[UITextField alloc]initWithFrame:CGRectMake(30.0, offstY, self.scrcollView.frame.size.width - 60.0, 28.0)];
  174. _urlTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  175. _urlTextField.layer.borderWidth = 1.0;
  176. _urlTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
  177. _urlTextField.layer.cornerRadius = 5.0;
  178. _urlTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  179. _urlTextField.leftViewMode = UITextFieldViewModeAlways;
  180. _urlTextField.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  181. _urlTextField.rightViewMode = UITextFieldViewModeAlways;
  182. _urlTextField.delegate = self;
  183. _urlTextField.hidden = YES;
  184. _urlTextField.font = [UIFont systemFontOfSize:18.0];
  185. _urlTextField.placeholder = @"https://www.compdf.com";
  186. [self.scrcollView addSubview:_urlTextField];
  187. _pageTextField = [[UITextField alloc]initWithFrame:CGRectMake(30.0, offstY, self.view.frame.size.width - 60.0, 28.0)];
  188. _pageTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  189. _pageTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  190. _pageTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
  191. _pageTextField.leftViewMode = UITextFieldViewModeAlways;
  192. _pageTextField.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  193. _pageTextField.rightViewMode = UITextFieldViewModeAlways;
  194. _pageTextField.hidden = YES;
  195. _pageTextField.layer.borderWidth = 1.0;
  196. _pageTextField.layer.cornerRadius = 5.0;
  197. _pageTextField.delegate = self;
  198. _pageTextField.font = [UIFont systemFontOfSize:18.0];
  199. [_pageTextField setKeyboardType:UIKeyboardTypeNumberPad];
  200. [self.scrcollView addSubview:_pageTextField];
  201. _emailTextField = [[UITextField alloc]initWithFrame:CGRectMake(30.0, offstY, self.view.frame.size.width - 60.0, 28.0)];
  202. _emailTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  203. _emailTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  204. _emailTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
  205. _emailTextField.leftViewMode = UITextFieldViewModeAlways;
  206. _emailTextField.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  207. _emailTextField.rightViewMode = UITextFieldViewModeAlways;
  208. _emailTextField.hidden = YES;
  209. _emailTextField.layer.borderWidth = 1.0;
  210. _emailTextField.layer.cornerRadius = 5.0;
  211. _emailTextField.delegate = self;
  212. _emailTextField.font = [UIFont systemFontOfSize:18.0];
  213. [self.scrcollView addSubview:_emailTextField];
  214. offstY +=_urlTextField.frame.size.height;
  215. [_emailTextField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
  216. [_pageTextField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
  217. [_urlTextField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
  218. offstY+= 30.0;
  219. self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
  220. self.saveButton.frame = CGRectMake((self.scrcollView.frame.size.width - 120)/2, offstY, 120, 32);
  221. self.saveButton.layer.cornerRadius = 5.0;
  222. self.saveButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  223. [self.saveButton setTitle:NSLocalizedString(@"Save",nil) forState:UIControlStateNormal];
  224. [self.saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  225. [self.saveButton addTarget:self action:@selector(buttonItemClicked_Save:) forControlEvents:UIControlEventTouchUpInside];
  226. self.saveButton.backgroundColor = [UIColor systemBlueColor];
  227. [self.scrcollView addSubview:self.saveButton];
  228. offstY += self.saveButton.frame.size.height;
  229. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
  230. [[NSNotificationCenter defaultCenter] addObserver:self
  231. selector:@selector(keyboardwillChangeFrame:)
  232. name:UIKeyboardWillChangeFrameNotification
  233. object:nil];
  234. [[NSNotificationCenter defaultCenter] addObserver:self
  235. selector:@selector(keyboardWillHide:)
  236. name:UIKeyboardWillHideNotification
  237. object:nil];
  238. }
  239. #pragma mark - Action
  240. - (void)buttonItemClicked_back:(id)sender {
  241. [self dismissViewControllerAnimated:YES completion:nil];
  242. }
  243. - (void)segmentedControlValueChanged_Mode:(id)sender {
  244. UITextField *currentTextField = nil;
  245. switch (self.segmentedControl.selectedSegmentIndex) {
  246. case 0:
  247. self.linkType = CPDFLinkTypeLink;
  248. currentTextField = self.urlTextField;
  249. break;
  250. case 1:
  251. self.linkType = CPDFLinkTypePage;
  252. currentTextField = self.pageTextField;
  253. break;
  254. default:
  255. case 2:
  256. self.linkType = CPDFLinkTypeEmail;
  257. currentTextField = self.emailTextField;
  258. break;
  259. }
  260. }
  261. - (void)buttonItemClicked_Save:(id)sender {
  262. NSString *string = nil;
  263. if(CPDFLinkTypeLink == self.linkType) {
  264. string = self.urlTextField.text;
  265. string = [string lowercaseString];
  266. if (![string hasPrefix:@"https://"] && ![string hasPrefix:@"http://"]) {
  267. string = [NSString stringWithFormat:@"https://%@",string];
  268. }
  269. } else if (CPDFLinkTypePage == self.linkType) {
  270. string = self.pageTextField.text;
  271. CPDFAnnotation *annotation = self.annotStyle.annotations.firstObject;
  272. CPDFDocument *document = annotation.page.document;
  273. if([string integerValue] > document.pageCount || [string intValue] < 1) {
  274. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  275. message:NSLocalizedString(@"Config Error", nil)
  276. preferredStyle:UIAlertControllerStyleAlert];
  277. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  278. self.pageTextField.text = @"";
  279. }];
  280. [alert addAction:okAction];
  281. [self presentViewController:alert animated:YES completion:nil];
  282. return;
  283. }
  284. } else if (CPDFLinkTypeEmail == self.linkType) {
  285. string = self.emailTextField.text;
  286. if (![string hasPrefix:@"mailto:"]) {
  287. string = [NSString stringWithFormat:@"mailto:%@",string];
  288. }
  289. }
  290. [self dismissViewControllerAnimated:YES completion:^{
  291. if([self.delegate respondsToSelector:@selector(linkViewController:linkType:linkString:)]) {
  292. [self.delegate linkViewController:self linkType:self.linkType linkString:string];
  293. }
  294. }];
  295. }
  296. #pragma mark - UITextFieldDelegate
  297. - (void)textFieldTextChange:(UITextField *)textField {
  298. if(textField.text.length > 0) {
  299. self.saveButton.enabled = YES;
  300. self.saveButton.backgroundColor = [UIColor systemBlueColor];
  301. [self.saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  302. } else {
  303. self.saveButton.enabled = NO;
  304. self.saveButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
  305. [self.saveButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  306. }
  307. }
  308. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  309. [textField resignFirstResponder];
  310. return YES;
  311. }
  312. #pragma mark - NSNotification
  313. - (void)keyboardwillChangeFrame:(NSNotification *)notification {
  314. NSDictionary *userInfo = [notification userInfo];
  315. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  316. CGRect frame = value.CGRectValue;
  317. CGRect rect = [self.urlTextField convertRect:self.urlTextField.frame toView:self.view];
  318. if(CGRectGetMaxY(rect) > self.view.frame.size.height - frame.size.height) {
  319. UIEdgeInsets insets = self.scrcollView.contentInset;
  320. insets.bottom = frame.size.height + self.urlTextField.frame.size.height;
  321. self.scrcollView.contentInset = insets;
  322. }
  323. }
  324. - (void)keyboardWillHide:(NSNotification *)notification {
  325. UIEdgeInsets insets = self.scrcollView.contentInset;
  326. insets.bottom = 0;
  327. self.scrcollView.contentInset = insets;
  328. }
  329. @end