CStampTextViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // CStampTextViewController.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 "CStampTextViewController.h"
  13. #import "CStampPreview.h"
  14. #import "CStampShapView.h"
  15. #import "CPDFColorSelectView.h"
  16. #import "CStampColorSelectView.h"
  17. #import "CPDFColorPickerView.h"
  18. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  19. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  20. #else
  21. #import "ComPDFKit_Tools.h"
  22. #endif
  23. @interface CStampTextViewController () <UITextFieldDelegate, CStampColorSelectViewDelegate, CStampShapViewDelegate>
  24. @property (nonatomic, strong) CStampPreview *preView;
  25. @property (nonatomic, strong) UITextField *stampTextField;
  26. @property (nonatomic, strong) UISwitch *haveDateSwitch;
  27. @property (nonatomic, strong) UISwitch *haveTimeSwitch;
  28. @property (nonatomic, strong) UIButton *doneBtn;
  29. @property (nonatomic, strong) UILabel *titleLabel;
  30. @property (nonatomic, strong) UIButton *cancelBtn;
  31. @property (nonatomic, strong) UILabel *dateLabel;
  32. @property (nonatomic, strong) UILabel *timeLabel;
  33. @property (nonatomic, strong) CStampColorSelectView *colorView;
  34. @property (nonatomic, strong) CStampShapView *stampShapeViw;
  35. @property (nonatomic, assign) NSInteger textStampColorStyle;
  36. @property (nonatomic, assign) NSInteger textStampStyle;
  37. @property (nonatomic, strong) UIScrollView *scrcollView;
  38. @end
  39. @implementation CStampTextViewController
  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;
  45. self.titleLabel.text = NSLocalizedString(@"Create Stamp", 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.doneBtn = [[UIButton alloc] init];
  51. self.doneBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  52. [self.doneBtn setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
  53. [self.doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  54. [self.doneBtn addTarget:self action:@selector(buttonItemClicked_done:) forControlEvents:UIControlEventTouchUpInside];
  55. [self.view addSubview:self.doneBtn];
  56. self.scrcollView = [[UIScrollView alloc] init];
  57. self.scrcollView.scrollEnabled = YES;
  58. self.scrcollView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  59. [self.view addSubview:self.scrcollView];
  60. self.cancelBtn = [[UIButton alloc] init];
  61. self.cancelBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  62. self.cancelBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  63. [self.cancelBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
  64. [self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  65. [self.cancelBtn addTarget:self action:@selector(buttonItemClicked_cancel:) forControlEvents:UIControlEventTouchUpInside];
  66. [self.view addSubview:self.cancelBtn];
  67. self.preView = [[CStampPreview alloc] init];
  68. self.preView.backgroundColor = [UIColor clearColor];
  69. self.preView.color = [UIColor blackColor];
  70. self.preView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  71. self.preView.layer.borderWidth = 1.0;
  72. self.preView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  73. [self.scrcollView addSubview:self.preView];
  74. self.stampTextField = [[UITextField alloc] init];
  75. self.stampTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  76. self.stampTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  77. self.stampTextField.leftViewMode = UITextFieldViewModeAlways;
  78. self.stampTextField.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
  79. self.stampTextField.rightViewMode = UITextFieldViewModeAlways;
  80. self.stampTextField.returnKeyType = UIReturnKeyDone;
  81. self.stampTextField.clearButtonMode = YES;
  82. self.stampTextField.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  83. self.stampTextField.layer.borderWidth = 1.0;
  84. self.stampTextField.delegate = self;
  85. self.stampTextField.borderStyle = UITextBorderStyleNone;
  86. [self.stampTextField addTarget:self action:@selector(textFieldEditChange:) forControlEvents:UIControlEventEditingChanged];
  87. self.stampTextField.placeholder = NSLocalizedString(@"Text", nil);
  88. [self.scrcollView addSubview:self.stampTextField];
  89. self.colorView = [[CStampColorSelectView alloc] init];
  90. self.colorView.delegate = self;
  91. self.colorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  92. self.colorView.selectedColor = [UIColor blackColor];
  93. [self.scrcollView addSubview:self.colorView];
  94. self.stampShapeViw = [[CStampShapView alloc] init];
  95. self.stampShapeViw.delegate = self;
  96. self.stampShapeViw.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  97. [self.scrcollView addSubview:self.stampShapeViw];
  98. self.dateLabel = [[UILabel alloc] init];
  99. self.dateLabel.text = NSLocalizedString(@"Date", nil);
  100. self.dateLabel.textColor = [UIColor grayColor];
  101. self.dateLabel.font = [UIFont systemFontOfSize:12.0];
  102. [self.scrcollView addSubview:self.dateLabel];
  103. self.haveDateSwitch = [[UISwitch alloc] init];
  104. [self.haveDateSwitch setOn:NO animated:NO];
  105. self.haveDateSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  106. [self.haveDateSwitch addTarget:self action:@selector(switchChange_date:) forControlEvents:UIControlEventValueChanged];
  107. [self.scrcollView addSubview:self.haveDateSwitch];
  108. self.timeLabel = [[UILabel alloc] init];
  109. self.timeLabel.text = NSLocalizedString(@"Time", nil);
  110. self.timeLabel.textColor = [UIColor grayColor];
  111. self.timeLabel.font = [UIFont systemFontOfSize:12.0];
  112. self.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  113. [self.scrcollView addSubview:self.timeLabel];
  114. self.haveTimeSwitch = [[UISwitch alloc] init];
  115. [self.haveTimeSwitch setOn:NO animated:NO];
  116. self.haveTimeSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  117. [self.haveTimeSwitch addTarget:self action:@selector(switchChange_time:) forControlEvents:UIControlEventValueChanged];
  118. [self.scrcollView addSubview:self.haveTimeSwitch];
  119. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  120. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  121. }
  122. - (void)viewWillLayoutSubviews {
  123. [super viewWillLayoutSubviews];
  124. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  125. self.scrcollView.frame = CGRectMake(0, 50, self.view.frame.size.width, 560);
  126. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 700);
  127. self.preView.frame = CGRectMake((self.view.frame.size.width - 350)/2, 15, 350, 120);
  128. self.stampTextField.frame = CGRectMake((self.view.frame.size.width - 350)/2, 150, 350, 30);
  129. if (@available(iOS 11.0, *)) {
  130. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  131. self.cancelBtn.frame = CGRectMake(self.view.safeAreaInsets.left + 10, 5, 50, 50);
  132. self.stampShapeViw.frame = CGRectMake(self.view.safeAreaInsets.left, 180,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  133. self.colorView.frame = CGRectMake(self.view.safeAreaInsets.left, 270,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 60);
  134. self.dateLabel.frame = CGRectMake(self.view.safeAreaInsets.left+20, 330, 80, 50);
  135. self.haveDateSwitch.frame = CGRectMake(self.view.frame.size.width - 80- self.view.safeAreaInsets.right, 330, 60, 50);
  136. self.timeLabel.frame = CGRectMake(self.view.safeAreaInsets.left+20, 380, 100, 45);
  137. self.haveTimeSwitch.frame = CGRectMake(self.view.frame.size.width - 80- self.view.safeAreaInsets.right, 380, 60, 50);
  138. } else {
  139. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  140. self.colorView.frame = CGRectMake(0, 270, self.view.frame.size.width, 60);
  141. self.stampShapeViw.frame = CGRectMake(0, 180, self.view.frame.size.width, 90);
  142. self.dateLabel.frame = CGRectMake(20, 330, 80, 50);
  143. self.haveDateSwitch.frame = CGRectMake(self.view.frame.size.width - 80, 330, 60, 50);
  144. self.timeLabel.frame = CGRectMake(20, 380, 100, 45);
  145. self.haveTimeSwitch.frame = CGRectMake(self.view.frame.size.width - 80, 380, 60, 50);
  146. }
  147. }
  148. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  149. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  150. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  151. }
  152. #pragma mark - Private Mthods
  153. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  154. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 560);
  155. }
  156. #pragma mark - Action
  157. - (void)buttonItemClicked_done:(id)sender {
  158. NSMutableDictionary *tStampItem = [[NSMutableDictionary alloc] init];
  159. if (![self.stampTextField.text isEqualToString:@""] || [self.haveDateSwitch isOn] || [self.haveTimeSwitch isOn]) {
  160. if (self.stampTextField.text.length > 0) {
  161. [tStampItem setObject:self.stampTextField.text forKey:@"text"];
  162. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampColorStyle] forKey:@"colorStyle"];
  163. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampStyle] forKey:@"style"];
  164. [tStampItem setObject:[NSNumber numberWithBool:[self.haveDateSwitch isOn]] forKey:@"haveDate"];
  165. [tStampItem setObject:[NSNumber numberWithBool:[self.haveTimeSwitch isOn]] forKey:@"haveTime"];
  166. } else {
  167. [tStampItem setObject:self.preView.dateTime forKey:@"text"];
  168. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampColorStyle] forKey:@"colorStyle"];
  169. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampStyle] forKey:@"style"];
  170. [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveDate"];
  171. [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveTime"];
  172. }
  173. if (self.delegate && [self.delegate respondsToSelector:@selector(stampTextViewController:dictionary:)]) {
  174. [self.delegate stampTextViewController:self dictionary:tStampItem];
  175. }
  176. } else if ([self.stampTextField.text isEqualToString:@""] && ![self.haveDateSwitch isOn] && ![self.haveTimeSwitch isOn]) {
  177. [tStampItem setObject:@"StampText" forKey:@"text"];
  178. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampColorStyle] forKey:@"colorStyle"];
  179. [tStampItem setObject:[NSNumber numberWithInteger:self.textStampStyle] forKey:@"style"];
  180. [tStampItem setObject:[NSNumber numberWithBool:[self.haveDateSwitch isOn]] forKey:@"haveDate"];
  181. [tStampItem setObject:[NSNumber numberWithBool:[self.haveTimeSwitch isOn]] forKey:@"haveTime"];
  182. if (self.delegate && [self.delegate respondsToSelector:@selector(stampTextViewController:dictionary:)]) {
  183. [self.delegate stampTextViewController:self dictionary:tStampItem];
  184. }
  185. }
  186. [self dismissViewControllerAnimated:YES completion:nil];
  187. }
  188. - (void)buttonItemClicked_cancel:(id)sender {
  189. [self dismissViewControllerAnimated:YES completion:nil];
  190. }
  191. - (void)textFieldEditChange:(id)sender {
  192. UITextField *textField = (UITextField *)sender;
  193. self.preView.textStampText = textField.text;
  194. [self.preView setNeedsDisplay];
  195. }
  196. - (void)switchChange_date:(id)sender {
  197. self.preView.textStampHaveDate = [self.haveDateSwitch isOn];
  198. [self.preView setNeedsDisplay];
  199. }
  200. - (void)switchChange_time:(id)sender {
  201. self.preView.textStampHaveTime = [self.haveTimeSwitch isOn];
  202. [self.preView setNeedsDisplay];
  203. }
  204. #pragma mark - UITextFieldDelegate
  205. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  206. if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
  207. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 800);
  208. } else {
  209. }
  210. }
  211. - (void)textFieldDidEndEditing:(UITextField *)textField {
  212. if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
  213. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 560);
  214. } else {
  215. }
  216. }
  217. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  218. [textField resignFirstResponder];
  219. return YES;
  220. }
  221. #pragma mark - CStampShapViewDelegate
  222. - (void)stampShapView:(CStampShapView *)stampShapView tag:(NSInteger)tag {
  223. switch (tag) {
  224. case TextStampTypeCenter:
  225. {
  226. self.preView.textStampStyle = TextStampTypeCenter;
  227. self.textStampStyle = TextStampTypeCenter;
  228. [self.preView setNeedsDisplay];
  229. }
  230. break;
  231. case TextStampTypeLeft:
  232. {
  233. self.preView.textStampStyle = TextStampTypeLeft;
  234. self.textStampStyle = TextStampTypeLeft;
  235. [self.preView setNeedsDisplay];
  236. }
  237. break;
  238. case TextStampTypeRight:
  239. {
  240. self.preView.textStampStyle = TextStampTypeRight;
  241. self.textStampStyle = TextStampTypeRight;
  242. [self.preView setNeedsDisplay];
  243. }
  244. break;
  245. case TextStampTypeNone:
  246. {
  247. self.preView.textStampStyle = TextStampTypeNone;
  248. self.textStampStyle = TextStampTypeNone;
  249. [self.preView setNeedsDisplay];
  250. }
  251. break;
  252. }
  253. }
  254. #pragma mark - CPDFColorSelectViewDelegate
  255. - (void)stampColorSelectView:(CStampColorSelectView *)StampColorSelectView tag:(NSInteger)tag {
  256. switch (tag) {
  257. case 0:
  258. {
  259. self.preView.textStampColorStyle = TextStampColorTypeBlack;
  260. self.textStampColorStyle = TextStampColorTypeBlack;
  261. [self.preView setNeedsDisplay];
  262. }
  263. break;
  264. case 1:
  265. {
  266. self.preView.textStampColorStyle = TextStampColorTypeRed;
  267. self.textStampColorStyle = TextStampColorTypeRed;
  268. [self.preView setNeedsDisplay];
  269. }
  270. break;
  271. case 2:
  272. {
  273. self.preView.textStampColorStyle = TextStampColorTypeGreen;
  274. self.textStampColorStyle = TextStampColorTypeGreen;
  275. [self.preView setNeedsDisplay];
  276. }
  277. break;
  278. case 3:
  279. {
  280. self.preView.textStampColorStyle = TextStampColorTypeBlue;
  281. self.textStampColorStyle = TextStampColorTypeBlue;
  282. [self.preView setNeedsDisplay];
  283. }
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. @end