CPDFStampViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //
  2. // CPDFStampViewController.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 "CPDFStampViewController.h"
  13. #import "CStampFileManger.h"
  14. #import "CStampCollectionViewCell.h"
  15. #import "CPDFColorUtils.h"
  16. #import "CStampButton.h"
  17. #import "CStampTextViewController.h"
  18. #import "CStampTextViewController.h"
  19. #import "CCustomizeStampTableViewCell.h"
  20. #import "CStampPreview.h"
  21. #import <ComPDFKit/ComPDFKit.h>
  22. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  23. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  24. #else
  25. #import "ComPDFKit_Tools.h"
  26. #endif
  27. #define kStamp_Cell_Height 60
  28. PDFAnnotationStampKey const PDFAnnotationStampKeyType = @"PDFAnnotationStampKeyType";
  29. PDFAnnotationStampKey const PDFAnnotationStampKeyImagePath = @"PDFAnnotationStampKeyImagePath";
  30. PDFAnnotationStampKey const PDFAnnotationStampKeyText = @"PDFAnnotationStampKeyText";
  31. PDFAnnotationStampKey const PDFAnnotationStampKeyShowDate = @"PDFAnnotationStampKeyShowDate";
  32. PDFAnnotationStampKey const PDFAnnotationStampKeyShowTime = @"PDFAnnotationStampKeyShowTime";
  33. PDFAnnotationStampKey const PDFAnnotationStampKeyStyle = @"PDFAnnotationStampKeyStyle";
  34. PDFAnnotationStampKey const PDFAnnotationStampKeyShape = @"PDFAnnotationStampKeyShape";
  35. @interface CPDFStampViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIPopoverPresentationControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITableViewDelegate, UITableViewDataSource, CStampTextViewControllerDelegate, CCustomizeStampTableViewCellDelegate>
  36. @property (nonatomic, strong) UICollectionView *collectView;
  37. @property (nonatomic, strong) UITableView *tableView;
  38. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  39. @property (nonatomic, strong) NSArray *standardArray;
  40. @property (nonatomic, strong) NSArray *customTextArray;
  41. @property (nonatomic, strong) NSArray *customImageArray;
  42. @property (nonatomic, strong) NSMutableDictionary *imgDicCache;
  43. @property (nonatomic, strong) UIButton *backBtn;
  44. @property (nonatomic, strong) UILabel *titleLabel;
  45. @property (nonatomic, strong) UIButton *createButton;
  46. @property (nonatomic, strong) UILabel *emptyLabel;
  47. @property (nonatomic, strong) UIView *standardView;
  48. @property (nonatomic, strong) UIView *customizeView;
  49. @property (nonatomic, strong) CStampFileManger *stampFileManager;
  50. @property (nonatomic, strong) CStampButton *textButton;
  51. @property (nonatomic, strong) CStampButton *imageButton;
  52. @property (nonatomic, strong) UIView *modelView;
  53. @property (nonatomic, strong) UIView *headerView;
  54. @end
  55. @implementation CPDFStampViewController
  56. #pragma mark - ViewController Methods
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. // Do any additional setup after loading the view.
  60. self.view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  61. self.headerView = [[UIView alloc] init];
  62. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  63. self.headerView.layer.borderWidth = 1.0;
  64. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  65. [self.view addSubview:self.headerView];
  66. self.titleLabel = [[UILabel alloc] init];
  67. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  68. self.titleLabel.text = NSLocalizedString(@"Stamp", nil);
  69. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  70. self.titleLabel.font = [UIFont systemFontOfSize:20];
  71. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  72. [self.headerView addSubview:self.titleLabel];
  73. self.backBtn = [[UIButton alloc] init];
  74. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  75. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  76. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  77. [self.headerView addSubview:self.backBtn];
  78. NSArray *segmmentArray = [NSArray arrayWithObjects:NSLocalizedString(@"Standard", nil), NSLocalizedString(@"Custom", nil), nil];
  79. self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmmentArray];
  80. self.segmentedControl.selectedSegmentIndex = 0;
  81. self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  82. [self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged_singature:) forControlEvents:UIControlEventValueChanged];
  83. [self.view addSubview:self.segmentedControl];
  84. self.stampFileManager = [[CStampFileManger alloc] init];
  85. [self.stampFileManager readStampDataFromFile];
  86. self.customTextArray = [self.stampFileManager getTextStampData];
  87. self.customImageArray = [self.stampFileManager getImageStampData];
  88. // StandardView
  89. [self createStandardView];
  90. // CustomizeView
  91. [self createCustomizeView];
  92. // Data
  93. NSMutableArray *array = [NSMutableArray array];
  94. for (int i=1; i<13; i++) {
  95. NSString *tPicName = nil;
  96. if (i<10) {
  97. tPicName = [NSString stringWithFormat:@"CPDFStampImage-0%d.png",i];
  98. } else {
  99. tPicName = [NSString stringWithFormat:@"CPDFStampImage-%d.png",i];
  100. }
  101. [array addObject:tPicName];
  102. }
  103. [array addObjectsFromArray:@[@"CPDFStampImage-13", @"CPDFStampImage-14", @"CPDFStampImage-15", @"CPDFStampImage-16", @"CPDFStampImage-20", @"CPDFStampImage-18", @"CPDFStampImage_chick", @"CPDFStampImage_cross", @"CPDFStampImage_circle"]];
  104. self.standardArray = array;
  105. self.imgDicCache = [NSMutableDictionary dictionary];
  106. [self createGestureRecognizer];
  107. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  108. }
  109. - (void)viewWillLayoutSubviews {
  110. [super viewWillLayoutSubviews];
  111. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 0, 120, 50);
  112. self.segmentedControl.frame = CGRectMake(50, 55, self.view.frame.size.width-100, 30);
  113. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
  114. self.emptyLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, (self.view.frame.size.height - 50)/2, 120, 50);
  115. if (@available(iOS 11.0, *)) {
  116. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  117. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 200 - self.view.safeAreaInsets.bottom, 50, 50);
  118. self.textButton.frame = CGRectMake(self.view.frame.size.width - 180 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 320 - self.view.safeAreaInsets.bottom, 160, 40);
  119. self.imageButton.frame = CGRectMake(self.view.frame.size.width - 180 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 270 - self.view.safeAreaInsets.bottom, 160, 40);
  120. } else {
  121. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  122. self.createButton.frame = CGRectMake(self.view.frame.size.width - 60, self.view.frame.size.height - 200, 50, 50);
  123. self.textButton.frame = CGRectMake(self.view.frame.size.width - 180, self.view.frame.size.height - 320, 160, 40);
  124. self.imageButton.frame = CGRectMake(self.view.frame.size.width - 180, self.view.frame.size.height - 270, 160, 40);
  125. }
  126. self.modelView.frame = CGRectMake(0, -200, self.view.bounds.size.width, self.view.bounds.size.height+200);
  127. }
  128. - (void)viewWillAppear:(BOOL)animated {
  129. [super viewWillAppear:animated];
  130. [self.collectView reloadData];
  131. }
  132. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  133. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  134. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  135. }
  136. #pragma mark - Private Methods
  137. - (void)createStandardView {
  138. self.standardView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100)];
  139. self.standardView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  140. [self.view addSubview:self.standardView];
  141. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  142. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  143. layout.minimumInteritemSpacing = 10;
  144. layout.minimumLineSpacing = 10;
  145. layout.itemSize = CGSizeMake(170,80);
  146. layout.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
  147. self.collectView = [[UICollectionView alloc] initWithFrame:self.standardView.bounds collectionViewLayout:layout];
  148. self.collectView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  149. self.collectView.delegate = self;
  150. self.collectView.dataSource = self;
  151. self.collectView.backgroundColor = [UIColor clearColor];
  152. [self.collectView registerClass:[StampCollectionHeaderView class]
  153. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  154. withReuseIdentifier:@"header"];
  155. [self.collectView registerClass:[StampCollectionHeaderView1 class]
  156. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  157. withReuseIdentifier:@"header1"];
  158. [self.collectView registerClass:[CStampCollectionViewCell class]
  159. forCellWithReuseIdentifier:@"TStampViewCell"];
  160. if (@available(iOS 11.0, *)) {
  161. _collectView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
  162. }
  163. [self.standardView addSubview:self.collectView];
  164. }
  165. - (void)createCustomizeView {
  166. self.customizeView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100)];
  167. self.customizeView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  168. [self.view addSubview:self.customizeView];
  169. self.customizeView.hidden = YES;
  170. self.tableView = [[UITableView alloc] initWithFrame:self.customizeView.bounds style:UITableViewStyleGrouped];
  171. self.tableView.delegate = self;
  172. self.tableView.dataSource = self;
  173. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  174. self.tableView.backgroundColor = [UIColor clearColor];
  175. self.tableView.rowHeight = 60;
  176. [self.customizeView addSubview:self.tableView];
  177. self.emptyLabel = [[UILabel alloc] init];
  178. self.emptyLabel.text = NSLocalizedString(@"NO Custom", nil);
  179. self.emptyLabel.textAlignment = NSTextAlignmentCenter;
  180. [self.customizeView addSubview:self.emptyLabel];
  181. if ((self.customImageArray.count < 1) && (self.customTextArray.count < 1)) {
  182. self.tableView.hidden = YES;
  183. self.emptyLabel.hidden = NO;
  184. } else {
  185. self.emptyLabel.hidden = YES;
  186. self.tableView.hidden = NO;
  187. }
  188. self.modelView = [[UIView alloc] init];
  189. self.modelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  190. [self.customizeView addSubview:self.modelView];
  191. self.modelView.hidden = YES;
  192. self.createButton = [[UIButton alloc] init];
  193. self.createButton.layer.cornerRadius = 25.0;
  194. self.createButton.clipsToBounds = YES;
  195. [self.createButton setImage:[UIImage imageNamed:@"CPDFSignatureImageAdd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  196. self.createButton.backgroundColor = [UIColor blueColor];
  197. [self.createButton addTarget:self action:@selector(buttonItemClicked_create:) forControlEvents:UIControlEventTouchUpInside];
  198. [self.customizeView addSubview:self.createButton];
  199. self.textButton = [[CStampButton alloc] init];
  200. [self.textButton.stampBtn setImage:[UIImage imageNamed:@"CPDFStampImageText" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  201. self.textButton.titleLabel.text = NSLocalizedString(@"Text Stamp", nil);
  202. [self.textButton.stampBtn addTarget:self action:@selector(buttonItemClicked_text:) forControlEvents:UIControlEventTouchUpInside];
  203. [self.customizeView addSubview:self.textButton];
  204. self.textButton.hidden = YES;
  205. self.imageButton = [[CStampButton alloc] init];
  206. [self.imageButton.stampBtn setImage:[UIImage imageNamed:@"CPDFStampImageImage" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  207. self.imageButton.titleLabel.text = NSLocalizedString(@"Image Stamp", nil);
  208. [self.imageButton.stampBtn addTarget:self action:@selector(buttonItemClicked_image:) forControlEvents:UIControlEventTouchUpInside];
  209. [self.customizeView addSubview:self.imageButton];
  210. self.imageButton.hidden = YES;
  211. }
  212. - (void)createGestureRecognizer {
  213. [self.createButton setUserInteractionEnabled:YES];
  214. [self.modelView setUserInteractionEnabled:YES];
  215. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panaddBookmarkBtn:)];
  216. [self.createButton addGestureRecognizer:panRecognizer];
  217. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapModelView:)];
  218. [self.modelView addGestureRecognizer:tapRecognizer];
  219. }
  220. - (void)panaddBookmarkBtn:(UIPanGestureRecognizer *)gestureRecognizer {
  221. CGPoint point = [gestureRecognizer translationInView:self.view];
  222. CGFloat newX = self.createButton.center.x + point.x;
  223. CGFloat newY = self.createButton.center.y + point.y;
  224. if (CGRectContainsPoint(self.view.frame, CGPointMake(newX, newY))) {
  225. self.createButton.center = CGPointMake(newX, newY);
  226. }
  227. [gestureRecognizer setTranslation:CGPointZero inView:self.view];
  228. }
  229. - (void)tapModelView:(UIPanGestureRecognizer *)gestureRecognizer {
  230. self.textButton.hidden = YES;
  231. self.modelView.hidden = YES;
  232. self.imageButton.hidden = YES;
  233. }
  234. - (void)createImageSignature {
  235. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  236. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  237. imagePickerController.delegate = self;
  238. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  239. [self presentViewController:imagePickerController animated:YES completion:nil];
  240. }];
  241. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Choose from Album", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  242. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  243. imagePickerController.delegate = self;
  244. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  245. imagePickerController.allowsEditing = YES;
  246. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  247. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  248. imagePickerController.popoverPresentationController.sourceView = self.imageButton;
  249. imagePickerController.popoverPresentationController.sourceRect = self.imageButton.bounds;
  250. }
  251. [self presentViewController:imagePickerController animated:YES completion:nil];
  252. }];
  253. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  254. style:UIAlertActionStyleCancel
  255. handler:nil];
  256. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
  257. message:nil
  258. preferredStyle:UIAlertControllerStyleActionSheet];
  259. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  260. actionSheet.popoverPresentationController.sourceView = self.imageButton;
  261. actionSheet.popoverPresentationController.sourceRect = self.imageButton.bounds;
  262. }
  263. [actionSheet addAction:cameraAction];
  264. [actionSheet addAction:photoAction];
  265. [actionSheet addAction:cancelAction];
  266. actionSheet.modalPresentationStyle = UIModalPresentationPopover;
  267. [self presentViewController:actionSheet animated:YES completion:nil];
  268. }
  269. - (UIImage *)compressImage:(UIImage *)image {
  270. CGFloat maxWH = kStamp_Cell_Height;
  271. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
  272. maxWH *=[UIScreen mainScreen].scale;
  273. CGFloat imageScale = 1.0;
  274. if (image.size.width > maxWH || image.size.height>maxWH)
  275. imageScale = MIN(maxWH / image.size.width, maxWH / image.size.height);
  276. CGSize newSize = CGSizeMake(image.size.width * imageScale, image.size.height * imageScale);
  277. UIGraphicsBeginImageContext(newSize);
  278. [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  279. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  280. UIGraphicsEndImageContext();
  281. return newImage;
  282. }
  283. #pragma mark - UIImagePickerControllerDelegate
  284. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  285. [picker dismissViewControllerAnimated:YES completion:nil];
  286. UIImage *image;
  287. if ([info objectForKey:UIImagePickerControllerEditedImage]) {
  288. image = [info objectForKey:UIImagePickerControllerEditedImage];
  289. } else if ([info objectForKey:UIImagePickerControllerOriginalImage]) {
  290. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  291. }
  292. UIImageOrientation imageOrientation = image.imageOrientation;
  293. if (imageOrientation!=UIImageOrientationUp) {
  294. UIGraphicsBeginImageContext(image.size);
  295. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  296. image = UIGraphicsGetImageFromCurrentImageContext();
  297. UIGraphicsEndImageContext();
  298. }
  299. NSData *imageData = UIImagePNGRepresentation(image);
  300. if (imageData == nil || [imageData length] <= 0) {
  301. return;
  302. }
  303. image = [UIImage imageWithData:imageData];
  304. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  305. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  306. if (imageRef) {
  307. image = [UIImage imageWithCGImage:imageRef];
  308. CGImageRelease(imageRef);
  309. }
  310. NSString *tPath = [self.stampFileManager saveStampWithImage:image];
  311. if (tPath) {
  312. NSMutableDictionary *tStampItem = [[NSMutableDictionary alloc] init];
  313. [tStampItem setObject:tPath forKey:@"path"];
  314. [self.stampFileManager insertStampItem:tStampItem type:PDFStampCustomType_Image];
  315. [self.tableView reloadData];
  316. if ((self.customImageArray.count < 1) && (self.customTextArray.count < 1)) {
  317. self.emptyLabel.hidden = NO;
  318. self.tableView.hidden = YES;
  319. } else {
  320. self.emptyLabel.hidden = YES;
  321. self.tableView.hidden = NO;
  322. }
  323. }
  324. }
  325. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  326. [picker dismissViewControllerAnimated:YES completion:nil];
  327. }
  328. #pragma mark - Protect Methods
  329. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  330. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  331. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  332. CGFloat mWidth = fmin(width, height);
  333. CGFloat mHeight = fmax(width, height);
  334. UIDevice *currentDevice = [UIDevice currentDevice];
  335. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  336. // This is an iPad
  337. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.5 : mHeight*0.6);
  338. } else {
  339. // This is an iPhone or iPod touch
  340. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.9 : mHeight*0.9);
  341. }
  342. }
  343. #pragma mark - Action
  344. - (void)buttonItemClicked_back:(id)sender {
  345. [self dismissViewControllerAnimated:YES completion:nil];
  346. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewControllerDismiss:)]) {
  347. [self.delegate stampViewControllerDismiss:self];
  348. }
  349. }
  350. - (void)buttonItemClicked_create:(id)sender {
  351. self.textButton.hidden = !self.textButton.hidden;
  352. self.modelView.hidden = !self.modelView.hidden;
  353. self.imageButton.hidden = !self.imageButton.hidden;
  354. }
  355. - (void)segmentedControlValueChanged_singature:(id)sender {
  356. if (self.segmentedControl.selectedSegmentIndex == 0) {
  357. self.standardView.hidden = NO;
  358. self.customizeView.hidden = YES;
  359. } else {
  360. self.standardView.hidden = YES;
  361. self.customizeView.hidden = NO;
  362. }
  363. }
  364. - (void)buttonItemClicked_text:(id)sender {
  365. self.textButton.hidden = YES;
  366. self.modelView.hidden = YES;
  367. self.imageButton.hidden = YES;
  368. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  369. CStampTextViewController *stampTextVC = [[CStampTextViewController alloc] init];
  370. stampTextVC.delegate = self;
  371. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:stampTextVC presentingViewController:self];
  372. stampTextVC.transitioningDelegate = presentationController;
  373. [self presentViewController:stampTextVC animated:YES completion:nil];
  374. }
  375. - (void)buttonItemClicked_image:(id)sender {
  376. self.textButton.hidden = YES;
  377. self.modelView.hidden = YES;
  378. self.imageButton.hidden = YES;
  379. [self createImageSignature];
  380. }
  381. #pragma mark - UICollectionViewDataSource
  382. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  383. return 1;
  384. }
  385. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  386. return self.standardArray.count;
  387. }
  388. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  389. CStampCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TStampViewCell" forIndexPath:indexPath];
  390. cell.editing = NO;
  391. cell.stampImage.image = [UIImage imageNamed:self.standardArray[indexPath.item] inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
  392. return cell;
  393. }
  394. #pragma mark - UICollectionViewDelegate
  395. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  396. [self dismissViewControllerAnimated:YES completion:^{
  397. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  398. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:[NSDictionary dictionary]];
  399. }
  400. }];
  401. }
  402. #pragma mark - UITableViewDataSource
  403. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  404. return 2;
  405. }
  406. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  407. switch (section) {
  408. case 0:
  409. {
  410. return self.customTextArray.count;
  411. }
  412. break;
  413. case 1:
  414. {
  415. return self.customImageArray.count;
  416. }
  417. break;
  418. default:
  419. return 0;
  420. }
  421. }
  422. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  423. if (section == 0) {
  424. return NSLocalizedString(@"Text Stamp", nil);
  425. } else if (section == 1) {
  426. return NSLocalizedString(@"Image Stamp", nil);
  427. } else {
  428. return @"";
  429. }
  430. }
  431. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  432. CCustomizeStampTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  433. if (cell == nil) {
  434. cell = [[CCustomizeStampTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  435. }
  436. if (self.customTextArray.count>0 || self.customImageArray.count>0) {
  437. if (0 == indexPath.section) {
  438. NSDictionary *tDic = _customTextArray[indexPath.item];
  439. NSString *tText = [tDic objectForKey:@"text"];
  440. NSInteger tStyle = [[tDic objectForKey:@"style"] integerValue];
  441. NSInteger tColorStyle = [[tDic objectForKey:@"colorStyle"] integerValue];
  442. BOOL tHaveDate = [[tDic objectForKey:@"haveDate"] boolValue];
  443. BOOL tHaveTime = [[tDic objectForKey:@"haveTime"] boolValue];
  444. CStampPreview *tPreview = [[CStampPreview alloc] initWithFrame:CGRectMake(0, 0, 320, kStamp_Cell_Height)];
  445. [tPreview setTextStampText:tText];
  446. [tPreview setTextStampColorStyle:tColorStyle];
  447. [tPreview setTextStampStyle:tStyle];
  448. [tPreview setTextStampHaveDate:tHaveDate];
  449. [tPreview setTextStampHaveTime:tHaveTime];
  450. tPreview.leftMargin = 0;
  451. UIImage *tImg = [tPreview renderImage];
  452. cell.customizeStampImageView.image = tImg;
  453. } else {
  454. NSDictionary *tDic = self.customImageArray[indexPath.item];
  455. UIImage *img = [self.imgDicCache objectForKey:tDic];
  456. if (!img) {
  457. NSString *tPath = [tDic objectForKey:@"path"];
  458. NSString *tFileName = [[NSFileManager defaultManager] displayNameAtPath:tPath];
  459. NSString *tRealPath = [NSString stringWithFormat:@"%@/%@",kPDFStampDataFolder,tFileName];
  460. UIImage *tImg = [UIImage imageWithContentsOfFile:tRealPath];
  461. img = [self compressImage:tImg];
  462. [self.imgDicCache setObject:img forKey:tDic];
  463. }
  464. cell.customizeStampImageView.image = img;
  465. }
  466. }
  467. cell.deleteDelegate = self;
  468. return cell;
  469. }
  470. #pragma mark - UITableViewDelegate
  471. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  472. if (indexPath.section == 0) {
  473. NSDictionary *tDic = _customTextArray[indexPath.item];
  474. NSString *tText = [tDic objectForKey:@"text"];
  475. NSInteger tStyle = [[tDic objectForKey:@"style"] integerValue];
  476. NSInteger tColorStyle = [[tDic objectForKey:@"colorStyle"] integerValue];
  477. BOOL tHaveDate = [[tDic objectForKey:@"haveDate"] boolValue];
  478. BOOL tHaveTime = [[tDic objectForKey:@"haveTime"] boolValue];
  479. NSInteger stampStype = 0;
  480. NSInteger stampShape = 0;
  481. switch (tColorStyle) {
  482. case TextStampColorTypeBlack:
  483. {
  484. stampStype = 0;
  485. switch (tStyle) {
  486. case TextStampTypeNone:
  487. {
  488. stampShape = 3;
  489. }
  490. break;
  491. case TextStampTypeRight:
  492. {
  493. stampShape = 2;
  494. }
  495. break;
  496. case TextStampTypeLeft:
  497. {
  498. stampShape = 1;
  499. }
  500. break;
  501. case TextStampTypeCenter:
  502. {
  503. stampShape = 0;
  504. }
  505. break;
  506. }
  507. }
  508. break;
  509. case TextStampColorTypeRed:
  510. {
  511. stampStype = 1;
  512. switch (tStyle) {
  513. case TextStampTypeNone:
  514. {
  515. stampShape = 3;
  516. }
  517. break;
  518. case TextStampTypeRight:
  519. {
  520. stampShape = 2;
  521. }
  522. break;
  523. case TextStampTypeLeft:
  524. {
  525. stampShape = 1;
  526. }
  527. break;
  528. case TextStampTypeCenter:
  529. {
  530. stampShape = 0;
  531. }
  532. break;
  533. }
  534. }
  535. break;
  536. case TextStampColorTypeGreen:
  537. {
  538. stampStype = 2;
  539. switch (tStyle) {
  540. case TextStampTypeNone:
  541. {
  542. stampShape = 3;
  543. }
  544. break;
  545. case TextStampTypeRight:
  546. {
  547. stampShape = 2;
  548. }
  549. break;
  550. case TextStampTypeLeft:
  551. {
  552. stampShape = 1;
  553. }
  554. break;
  555. case TextStampTypeCenter:
  556. {
  557. stampShape = 0;
  558. }
  559. break;
  560. }
  561. }
  562. break;
  563. case TextStampColorTypeBlue:
  564. {
  565. stampStype = 3;
  566. switch (tStyle) {
  567. case TextStampTypeNone:
  568. {
  569. stampShape = 3;
  570. }
  571. break;
  572. case TextStampTypeRight:
  573. {
  574. stampShape = 2;
  575. }
  576. break;
  577. case TextStampTypeLeft:
  578. {
  579. stampShape = 1;
  580. }
  581. break;
  582. case TextStampTypeCenter:
  583. {
  584. stampShape = 0;
  585. }
  586. break;
  587. }
  588. }
  589. break;
  590. }
  591. [self dismissViewControllerAnimated:YES completion:^{
  592. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  593. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:@{PDFAnnotationStampKeyText : tText,
  594. PDFAnnotationStampKeyShowDate : @(tHaveDate),
  595. PDFAnnotationStampKeyShowTime : @(tHaveTime),
  596. PDFAnnotationStampKeyStyle : @(stampStype),
  597. PDFAnnotationStampKeyShape : @(stampShape)}];
  598. }
  599. }];
  600. } else if (indexPath.section == 1) {
  601. NSDictionary *tDict = self.customImageArray[indexPath.row];
  602. NSString *tPath = [tDict objectForKey:@"path"];
  603. NSString *tFileName = [[NSFileManager defaultManager] displayNameAtPath:tPath];
  604. NSString *tRealPath = [NSString stringWithFormat:@"%@/%@",kPDFStampDataFolder,tFileName];
  605. [self dismissViewControllerAnimated:YES completion:^{
  606. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  607. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:@{PDFAnnotationStampKeyImagePath : tRealPath}];
  608. }
  609. }];
  610. }
  611. }
  612. #pragma mark - CCustomizeStampTableViewCellDelegate
  613. - (void)customizeStampTableViewCell:(CCustomizeStampTableViewCell *)customizeStampTableViewCell {
  614. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  615. style:UIAlertActionStyleCancel
  616. handler:nil];
  617. UIAlertAction *OKAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  618. style:UIAlertActionStyleDefault
  619. handler:^(UIAlertAction * _Nonnull action) {
  620. NSIndexPath *select = [self.tableView indexPathForCell:customizeStampTableViewCell];
  621. if (select.section == 0) {
  622. [self.stampFileManager removeStampItem:select.row type:PDFStampCustomType_Text];
  623. } else if (select.section == 1) {
  624. [self.stampFileManager removeStampItem:select.row type:PDFStampCustomType_Image];
  625. }
  626. [self.tableView reloadData];
  627. if ((self.customImageArray.count < 1) && (self.customTextArray.count < 1)) {
  628. self.emptyLabel.hidden = NO;
  629. self.tableView.hidden = YES;
  630. } else {
  631. self.emptyLabel.hidden = YES;
  632. self.tableView.hidden = NO;
  633. }
  634. }];
  635. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil)
  636. message:NSLocalizedString(@"Are you sure to delete?", nil)
  637. preferredStyle:UIAlertControllerStyleAlert];
  638. [alert addAction:cancelAction];
  639. [alert addAction:OKAction];
  640. [self presentViewController:alert animated:YES completion:nil];
  641. }
  642. #pragma mark - CStampTextViewControllerDelegate
  643. - (void)stampTextViewController:(CStampTextViewController *)stampTextViewController dictionary:(NSDictionary *)dictionary {
  644. [self.stampFileManager insertStampItem:dictionary type:PDFStampCustomType_Text];
  645. [self.tableView reloadData];
  646. if ((self.customImageArray.count < 1) && (self.customTextArray.count < 1)) {
  647. self.emptyLabel.hidden = NO;
  648. self.tableView.hidden = YES;
  649. } else {
  650. self.emptyLabel.hidden = YES;
  651. self.tableView.hidden = NO;
  652. }
  653. }
  654. @end