CPDFSignatureEditViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. //
  2. // CPDFSignatureEditViewController.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 "CPDFSignatureEditViewController.h"
  13. #import "CPDFColorSelectView.h"
  14. #import "CSignatureTopBar.h"
  15. #import "CSignatureDrawView.h"
  16. #import "CPDFSignatureEditViewController_Header.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. #define kKMSignayureTextMaxWidth 200
  23. @interface CPDFSignatureEditViewController () <UIPopoverPresentationControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIColorPickerViewControllerDelegate, UITextFieldDelegate,UIGestureRecognizerDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate, CSignatureDrawViewDelegate>
  24. @property (nonatomic, strong) CPDFColorSelectView *colorSelectView;
  25. @property (nonatomic, strong) UIButton *cacelButon;
  26. @property (nonatomic, strong) UIButton *saveButton;
  27. @property (nonatomic, strong) CSignatureDrawView *signatureDrawTextView;
  28. @property (nonatomic, strong) CSignatureDrawView *signatureDrawImageView;
  29. @property (nonatomic, strong) UITextField *textField;
  30. @property (nonatomic, strong) CALayer *bottomBorder;
  31. @property (nonatomic, strong) UIButton *createButton;
  32. @property (nonatomic, assign) CSignatureTopBarSelectedIndex selecIndex;
  33. @property (nonatomic, strong) UIView *thicknessView;
  34. @property (nonatomic, strong) UILabel *thicknessLabel;
  35. @property (nonatomic, strong) UISlider *thicknessSlider;
  36. @property (nonatomic, strong) UIButton *clearButton;
  37. @property (nonatomic, strong) UILabel *emptyLabel;
  38. @property (nonatomic, strong) UIView *headerView;
  39. @property (nonatomic, assign) BOOL isDrawSignature;
  40. @property (nonatomic, assign) BOOL isTexrSignature;
  41. @property (nonatomic, assign) BOOL isImageSignature;
  42. @end
  43. @implementation CPDFSignatureEditViewController
  44. #pragma mark - ViewController Methods
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // Do any additional setup after loading the view
  48. self.headerView = [[UIView alloc] init];
  49. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  50. self.headerView.layer.borderWidth = 1.0;
  51. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  52. [self.view addSubview:self.headerView];
  53. [self initSegmentedControl];
  54. self.emptyLabel = [[UILabel alloc] init];
  55. self.emptyLabel.font = [UIFont systemFontOfSize:22];
  56. self.emptyLabel.textColor = [UIColor grayColor];
  57. self.emptyLabel.text = NSLocalizedString(@"Enter your signature", nil);
  58. self.emptyLabel.textAlignment = NSTextAlignmentCenter;
  59. self.emptyLabel.adjustsFontSizeToFitWidth = YES;
  60. [self.view addSubview:self.emptyLabel];
  61. self.colorSelectView = [[CPDFColorSelectView alloc] init];
  62. [self.colorSelectView.colorLabel removeFromSuperview];
  63. self.colorSelectView.selectedColor = [UIColor blackColor];
  64. self.colorSelectView.delegate = self;
  65. [self.view addSubview:self.colorSelectView];
  66. self.thicknessView = [[UIView alloc] init];
  67. self.thicknessView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  68. [self.view addSubview:self.thicknessView];
  69. self.thicknessLabel = [[UILabel alloc] init];
  70. self.thicknessLabel.text = NSLocalizedString(@"Thickness", nil);
  71. self.thicknessLabel.textColor = [UIColor grayColor];
  72. self.thicknessLabel.font = [UIFont systemFontOfSize:12.0];
  73. [self.thicknessView addSubview:self.thicknessLabel];
  74. self.thicknessSlider = [[UISlider alloc] init];
  75. self.thicknessSlider.maximumValue = 20;
  76. self.thicknessSlider.minimumValue = 1;
  77. self.thicknessSlider.value = 5;
  78. [self.thicknessSlider addTarget:self action:@selector(buttonItemClicked_changes:) forControlEvents:UIControlEventValueChanged];
  79. [self.thicknessView addSubview:self.thicknessSlider];
  80. self.signatureDrawTextView = [[CSignatureDrawView alloc] init];
  81. self.signatureDrawTextView.delegate = self;
  82. self.signatureDrawTextView.color = [UIColor blackColor];
  83. self.signatureDrawTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  84. self.signatureDrawTextView.lineWidth = self.thicknessSlider.value;
  85. [self.view addSubview:self.signatureDrawTextView];
  86. self.signatureDrawImageView = [[CSignatureDrawView alloc] init];
  87. self.signatureDrawImageView.delegate = self;
  88. self.signatureDrawImageView.color = [UIColor blackColor];
  89. self.signatureDrawImageView.lineWidth = 4;
  90. self.signatureDrawImageView.userInteractionEnabled = NO;
  91. [self.view addSubview:self.signatureDrawImageView];
  92. self.signatureDrawImageView.hidden = YES;
  93. self.cacelButon = [[UIButton alloc] init];
  94. [self.cacelButon setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
  95. self.cacelButon.titleLabel.adjustsFontSizeToFitWidth = YES;
  96. [self.cacelButon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  97. [self.cacelButon addTarget:self action:@selector(buttonItemClicked_Cancel:) forControlEvents:UIControlEventTouchUpInside];
  98. [self.headerView addSubview:self.cacelButon];
  99. self.saveButton = [[UIButton alloc] init];
  100. [self.saveButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
  101. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  102. self.saveButton.enabled = NO;
  103. [self.saveButton addTarget:self action:@selector(buttonItemClicked_Save:) forControlEvents:UIControlEventTouchUpInside];
  104. [self.headerView addSubview:self.saveButton];
  105. self.bottomBorder = [CALayer layer];
  106. self.bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
  107. self.textField = [[UITextField alloc] init];
  108. self.textField.delegate = self;
  109. self.textField.textColor = [UIColor blackColor];
  110. self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
  111. self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
  112. self.textField.spellCheckingType = UITextSpellCheckingTypeNo;
  113. self.textField.placeholder = NSLocalizedString(@"Enter your signature", nil);
  114. self.textField.textAlignment = NSTextAlignmentCenter;
  115. self.textField.font = [UIFont systemFontOfSize:30];
  116. [self.textField addTarget:self action:@selector(textTextField_change:) forControlEvents:UIControlEventEditingChanged];
  117. [self.view addSubview:self.textField];
  118. [self.textField.layer addSublayer:self.bottomBorder];
  119. self.textField.hidden = YES;
  120. self.createButton = [[UIButton alloc] init];
  121. self.createButton.layer.cornerRadius = 25.0;
  122. self.createButton.clipsToBounds = YES;
  123. [self.createButton setImage:[UIImage imageNamed:@"CPDFSignatureImageAdd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  124. self.createButton.backgroundColor = [UIColor blueColor];
  125. [self.createButton addTarget:self action:@selector(buttonItemClicked_create:) forControlEvents:UIControlEventTouchUpInside];
  126. [self.view addSubview:self.createButton];
  127. self.createButton.hidden = YES;
  128. self.clearButton = [[UIButton alloc] init];
  129. [self.clearButton setImage:[UIImage imageNamed:@"CPDFSignatureImageClean" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  130. self.clearButton.titleLabel.adjustsFontSizeToFitWidth = YES;
  131. self.clearButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
  132. self.clearButton.layer.borderColor = [UIColor grayColor].CGColor;
  133. self.clearButton.layer.borderWidth = 1.0;
  134. self.clearButton.layer.cornerRadius = 25.0;
  135. self.clearButton.layer.masksToBounds = YES;
  136. [self.clearButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  137. [self.clearButton addTarget:self action:@selector(buttonItemClicked_clear:) forControlEvents:UIControlEventTouchUpInside];
  138. [self.view addSubview:self.clearButton];
  139. self.selecIndex = CSignatureTopBarDefault;
  140. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  141. [self createGestureRecognizer];
  142. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  143. self.isImageSignature = NO;
  144. self.isDrawSignature = NO;
  145. self.isImageSignature = NO;
  146. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
  147. }
  148. - (void)viewWillLayoutSubviews {
  149. [super viewWillLayoutSubviews];
  150. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
  151. self.segmentedControl.frame = CGRectMake((self.view.frame.size.width - 220)/2, 10, 220, 30);
  152. self.emptyLabel.frame = CGRectMake((self.view.frame.size.width - 200)/2, (self.view.frame.size.height - 50)/2, 200, 50);
  153. if (@available(iOS 11.0, *)) {
  154. UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  155. if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
  156. self.colorSelectView.frame = CGRectMake(self.view.safeAreaInsets.left, 50, 380, 60);
  157. self.colorSelectView.colorPickerView.frame = CGRectMake(0, 0, self.colorSelectView.frame.size.width, self.colorSelectView.frame.size.height);
  158. self.thicknessView.frame = CGRectMake(self.view.safeAreaInsets.left, 140, self.view.frame.size.width-self.view.safeAreaInsets.left-self.view.safeAreaInsets.right, 60);
  159. self.thicknessLabel.frame = CGRectMake(20, 15, 60, 30);
  160. self.thicknessSlider.frame = CGRectMake(90, 0, self.thicknessView.bounds.size.width-110, 60);
  161. self.signatureDrawTextView.frame = CGRectMake(self.view.safeAreaInsets.left, 210, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height-self.view.safeAreaInsets.top-self.view.safeAreaInsets.bottom-150);
  162. } else if (UIInterfaceOrientationIsLandscape(currentOrientation)) {
  163. self.colorSelectView.frame = CGRectMake(self.view.safeAreaInsets.left, 50, 380, 60);
  164. self.thicknessView.frame = CGRectMake(380, 70, self.view.frame.size.width-380-self.view.safeAreaInsets.right, 60);
  165. self.thicknessLabel.frame = CGRectMake(20, 15, 60, 30);
  166. self.thicknessSlider.frame = CGRectMake(90, 0, self.thicknessView.bounds.size.width-110, 60);
  167. self.signatureDrawTextView.frame = CGRectMake(self.view.safeAreaInsets.left, 130, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height-self.view.safeAreaInsets.top-self.view.safeAreaInsets.bottom-130);
  168. }
  169. self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 40);
  170. self.cacelButon.frame = CGRectMake( self.view.safeAreaInsets.left+20, 5, 50, 40);
  171. self.signatureDrawImageView.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-self.view.safeAreaInsets.top-self.view.safeAreaInsets.bottom-150);
  172. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70 - self.view.safeAreaInsets.right, self.view.frame.size.height - 100 - self.view.safeAreaInsets.bottom, 50, 50);
  173. self.clearButton.frame = CGRectMake(self.view.frame.size.width - 70 - self.view.safeAreaInsets.right, self.view.frame.size.height - 100 - self.view.safeAreaInsets.bottom, 50, 50);
  174. } else {
  175. UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  176. if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
  177. self.colorSelectView.frame = CGRectMake(10, 50, 380, 60);
  178. self.colorSelectView.colorPickerView.frame = CGRectMake(0, 0, self.colorSelectView.frame.size.width, self.colorSelectView.frame.size.height);
  179. self.thicknessView.frame = CGRectMake(10, 140, self.view.frame.size.width-20, 60);
  180. self.thicknessLabel.frame = CGRectMake(20, 15, 60, 30);
  181. self.thicknessSlider.frame = CGRectMake(90, 0, self.thicknessView.bounds.size.width-110, 60);
  182. self.signatureDrawTextView.frame = CGRectMake(10, 210, self.view.frame.size.width-20, self.view.frame.size.height-114-150);
  183. } else if (UIInterfaceOrientationIsLandscape(currentOrientation)) {
  184. self.colorSelectView.frame = CGRectMake(10, 50, 380, 60);
  185. self.thicknessView.frame = CGRectMake(380, 70, self.view.frame.size.width-380-10, 60);
  186. self.thicknessLabel.frame = CGRectMake(20, 15, 60, 30);
  187. self.thicknessSlider.frame = CGRectMake(90, 0, self.thicknessView.bounds.size.width-110, 60);
  188. self.signatureDrawTextView.frame = CGRectMake(10, 130, self.view.frame.size.width-20, self.view.frame.size.height-114-130);
  189. }
  190. self.signatureDrawImageView.frame = self.signatureDrawTextView.frame;
  191. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 100, 50, 50);
  192. self.clearButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 100, 50, 50);
  193. self.saveButton.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 40);
  194. self.cacelButon.frame = CGRectMake(20, 5, 50, 40);
  195. }
  196. self.textField.frame = CGRectMake((self.view.frame.size.width - 300)/2, 200, 300, 100);
  197. }
  198. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  199. if (self.segmentedControl.selectedSegmentIndex == 1) {
  200. [self.textField resignFirstResponder];
  201. } else if (self.segmentedControl.selectedSegmentIndex == 0 || self.segmentedControl.selectedSegmentIndex == 2) {
  202. [self.signatureDrawTextView signatureClear];
  203. }
  204. }
  205. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  206. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  207. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  208. }
  209. - (void)dealloc {
  210. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
  211. }
  212. #pragma mark - Private Methods
  213. - (void)initSegmentedControl {
  214. NSArray *segmmentArray = [NSArray arrayWithObjects:NSLocalizedString(@"Trackpad", nil), NSLocalizedString(@"Keyboard", nil), NSLocalizedString(@"Image", nil),nil];
  215. _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmmentArray];
  216. _segmentedControl.selectedSegmentIndex = 0;
  217. _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  218. [_segmentedControl addTarget:self action:@selector(segmentedControlValueChanged_singature:) forControlEvents:UIControlEventValueChanged];
  219. [self.view addSubview:self.segmentedControl];
  220. }
  221. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  222. if ([self.colorPicker superview]) {
  223. UIDevice *currentDevice = [UIDevice currentDevice];
  224. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  225. // This is an iPad
  226. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 520);
  227. } else {
  228. // This is an iPhone or iPod touch
  229. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 320);
  230. }
  231. } else {
  232. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  233. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  234. CGFloat mWidth = fmin(width, height);
  235. CGFloat mHeight = fmax(width, height);
  236. UIDevice *currentDevice = [UIDevice currentDevice];
  237. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  238. // This is an iPad
  239. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.5 : mHeight*0.6);
  240. } else {
  241. // This is an iPhone or iPod touch
  242. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.9 : mHeight*0.9);
  243. }
  244. }
  245. }
  246. - (void)createGestureRecognizer {
  247. [self.createButton setUserInteractionEnabled:YES];
  248. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panaddBookmarkBtn:)];
  249. [self.createButton addGestureRecognizer:panRecognizer];
  250. }
  251. - (void)panaddBookmarkBtn:(UIPanGestureRecognizer *)gestureRecognizer {
  252. CGPoint point = [gestureRecognizer translationInView:self.view];
  253. CGFloat newX = self.createButton.center.x + point.x;
  254. CGFloat newY = self.createButton.center.y + point.y;
  255. if (CGRectContainsPoint(self.view.frame, CGPointMake(newX, newY))) {
  256. self.createButton.center = CGPointMake(newX, newY);
  257. }
  258. [gestureRecognizer setTranslation:CGPointZero inView:self.view];
  259. }
  260. - (UIImage *)createTextSignature {
  261. if (self.textField.text.length < 1) {
  262. return [UIImage new];
  263. }
  264. NSString *string = self.textField.text;
  265. UIFont *font = self.textField.font;
  266. CGSize size = [self labelAutoCalculateRectWith:string Font:font];
  267. CGFloat sideH = size.height + 10;
  268. CGFloat w = size.width;
  269. if (w + 10 > kKMSignayureTextMaxWidth) {
  270. w = kKMSignayureTextMaxWidth - 10;
  271. }
  272. UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w + 10, sideH)];
  273. backView.backgroundColor = [UIColor clearColor];
  274. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, w, size.height)];
  275. label.font = font;
  276. label.text = string;
  277. label.textAlignment = NSTextAlignmentCenter;
  278. label.numberOfLines = 1;
  279. label.textColor = self.textField.textColor;
  280. label.adjustsFontSizeToFitWidth = YES;
  281. [backView addSubview:label];
  282. UIImage *newImage = [self imageWithUIView:backView];
  283. return newImage;
  284. }
  285. - (UIImage *)imageWithUIView:(UIView *) view {
  286. UIGraphicsBeginImageContextWithOptions(view.bounds.size, 0.0, 0.0);
  287. CGContextRef ctx = UIGraphicsGetCurrentContext();
  288. [view.layer renderInContext:ctx];
  289. UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
  290. UIGraphicsEndImageContext();
  291. return tImage;
  292. }
  293. - (CGSize)labelAutoCalculateRectWith:(NSString*)text Font:(UIFont*)font {
  294. NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  295. CGSize maxSize = CGSizeMake(NSNotFound, NSNotFound);
  296. paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
  297. NSDictionary* attributes =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
  298. CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
  299. labelSize.height = ceil(labelSize.height);
  300. labelSize.width = ceil(labelSize.width);
  301. return labelSize;
  302. }
  303. - (void)initDrawSignatureViewProperties {
  304. self.colorSelectView.hidden = NO;
  305. self.signatureDrawTextView.hidden = NO;
  306. self.selecIndex = CSignatureTopBarDefault;
  307. self.colorSelectView.hidden = NO;
  308. self.clearButton.hidden = NO;
  309. self.signatureDrawTextView.selectIndex = CSignatureDrawText;
  310. self.emptyLabel.hidden = NO;
  311. self.thicknessView.hidden = NO;
  312. self.signatureDrawImageView.hidden = YES;
  313. self.createButton.hidden = YES;
  314. self.textField.hidden = YES;
  315. [self.textField resignFirstResponder];
  316. if (self.isDrawSignature) {
  317. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  318. self.saveButton.enabled = YES;
  319. } else {
  320. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  321. self.saveButton.enabled = NO;
  322. }
  323. }
  324. - (void)initTextSignatureViewProperties {
  325. self.colorSelectView.hidden = NO;
  326. self.signatureDrawTextView.hidden = YES;
  327. self.signatureDrawImageView.hidden = YES;
  328. self.textField.hidden = NO;
  329. self.selecIndex = CSignatureTopBarText;
  330. self.colorSelectView.hidden = NO;
  331. self.createButton.hidden = YES;
  332. self.thicknessView.hidden = YES;
  333. self.clearButton.hidden = NO;
  334. self.emptyLabel.hidden = YES;
  335. [self.textField becomeFirstResponder];
  336. if (self.isTexrSignature) {
  337. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  338. self.saveButton.enabled = YES;
  339. } else {
  340. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  341. self.saveButton.enabled = NO;
  342. }
  343. }
  344. - (void)initImageSignatureViewProperties {
  345. [self.textField resignFirstResponder];
  346. self.colorSelectView.hidden = YES;
  347. self.signatureDrawTextView.hidden = YES;
  348. self.signatureDrawImageView.hidden = NO;
  349. self.textField.hidden = YES;
  350. self.selecIndex = CSignatureTopBarImage;
  351. self.createButton.hidden = NO;
  352. self.colorSelectView.hidden = YES;
  353. self.signatureDrawImageView.selectIndex = CSignatureDrawImage;
  354. self.thicknessView.hidden = YES;
  355. self.clearButton.hidden = YES;
  356. self.emptyLabel.hidden = YES;
  357. [self.signatureDrawImageView setNeedsDisplay];
  358. if (self.isImageSignature) {
  359. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  360. self.saveButton.enabled = YES;
  361. } else {
  362. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  363. self.saveButton.enabled = NO;
  364. }
  365. }
  366. #pragma mark - Action
  367. - (void)buttonItemClicked_Save:(id)sender {
  368. if (CSignatureTopBarDefault == self.selecIndex) {
  369. UIImage *image = [self.signatureDrawTextView signatureImage];
  370. if (self.delegate && [self.delegate respondsToSelector:@selector(signatureEditViewController:image:)]) {
  371. [self.delegate signatureEditViewController:self image:image];
  372. }
  373. } else if (CSignatureTopBarImage == self.selecIndex) {
  374. [self.signatureDrawTextView signatureClear];
  375. UIImage *image = [self.signatureDrawImageView signatureImage];
  376. if (self.delegate && [self.delegate respondsToSelector:@selector(signatureEditViewController:image:)]) {
  377. [self.delegate signatureEditViewController:self image:image];
  378. }
  379. }else if (CSignatureTopBarText == self.selecIndex) {
  380. UIImage *image = [self createTextSignature];
  381. if(self.textField.text.length == 0) {
  382. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Info" message:@"Please input Signature" preferredStyle:UIAlertControllerStyleAlert];
  383. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
  384. [alertController addAction:cancelAction];
  385. [self presentViewController:alertController animated:YES completion:nil];
  386. return;
  387. }
  388. if (self.textField.text) {
  389. if(self.delegate && [self.delegate respondsToSelector:@selector(signatureEditViewController:image:)]) {
  390. [self.delegate signatureEditViewController:self image:image];
  391. }
  392. }
  393. }
  394. // [self buttonItemClicked_Cancel:sender];
  395. }
  396. - (void)buttonItemClicked_Cancel:(id)sender {
  397. [self dismissViewControllerAnimated:YES completion:nil];
  398. }
  399. - (void)buttonItemClicked_create:(id)sender {
  400. [self createImageSignature];
  401. }
  402. - (void)segmentedControlValueChanged_singature:(id)sender {
  403. if (self.segmentedControl.selectedSegmentIndex == 0) {
  404. [self initDrawSignatureViewProperties];
  405. } else if (self.segmentedControl.selectedSegmentIndex == 1) {
  406. [self initTextSignatureViewProperties];
  407. } else if (self.segmentedControl.selectedSegmentIndex == 2) {
  408. [self initImageSignatureViewProperties];
  409. }
  410. }
  411. - (void)buttonItemClicked_changes:(UISlider *)sender {
  412. self.signatureDrawTextView.lineWidth = sender.value;
  413. [self.signatureDrawTextView setNeedsDisplay];
  414. }
  415. - (void)buttonItemClicked_clear:(UIButton *)button {
  416. if (self.segmentedControl.selectedSegmentIndex == 0) {
  417. [self.signatureDrawTextView signatureClear];
  418. self.emptyLabel.text = NSLocalizedString(@"Enter your signature", nil);
  419. self.isDrawSignature = NO;
  420. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  421. self.saveButton.enabled = NO;
  422. } else if (self.segmentedControl.selectedSegmentIndex == 1) {
  423. self.textField.text = @"";
  424. self.isTexrSignature = NO;
  425. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  426. self.saveButton.enabled = NO;
  427. }
  428. }
  429. - (void)textTextField_change:(UITextField *)textField {
  430. if (self.textField.text.length > 0) {
  431. self.isTexrSignature = YES;
  432. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  433. self.saveButton.enabled = YES;
  434. } else {
  435. self.isTexrSignature = NO;
  436. [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  437. self.saveButton.enabled = NO;
  438. }
  439. }
  440. #pragma mark - UITextFieldDelegate
  441. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  442. if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
  443. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  444. self.clearButton.center = CGPointMake(self.clearButton.center.x, self.clearButton.center.y-300);
  445. } completion:nil];
  446. } else {
  447. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  448. self.clearButton.center = CGPointMake(self.clearButton.center.x, self.clearButton.center.y-150);
  449. self.textField.center = CGPointMake(self.textField.center.x, self.textField.center.y-150);
  450. } completion:nil];
  451. self.colorSelectView.hidden = YES;
  452. }
  453. }
  454. - (void)textFieldDidEndEditing:(UITextField *)textField {
  455. if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
  456. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  457. self.clearButton.center = CGPointMake(self.clearButton.center.x, self.clearButton.center.y+300);
  458. } completion:nil];
  459. } else {
  460. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  461. self.clearButton.center = CGPointMake(self.clearButton.center.x, self.clearButton.center.y+150);
  462. self.textField.center = CGPointMake(self.textField.center.x, self.textField.center.y+150);
  463. } completion:nil];
  464. self.colorSelectView.hidden = NO;
  465. }
  466. }
  467. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  468. [textField resignFirstResponder];
  469. return YES;
  470. }
  471. #pragma mark - Private Methods
  472. - (void)createImageSignature {
  473. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  474. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  475. imagePickerController.delegate = self;
  476. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  477. [self presentViewController:imagePickerController animated:YES completion:nil];
  478. }];
  479. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Choose from Album", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  480. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  481. imagePickerController.delegate = self;
  482. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  483. imagePickerController.allowsEditing = YES;
  484. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  485. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  486. imagePickerController.popoverPresentationController.sourceView = self.segmentedControl;;
  487. imagePickerController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMaxX(self.segmentedControl.bounds), CGRectGetMaxY(self.segmentedControl.bounds), 1, 1);
  488. }
  489. [self presentViewController:imagePickerController animated:YES completion:nil];
  490. }];
  491. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  492. style:UIAlertActionStyleCancel
  493. handler:nil];
  494. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
  495. message:nil
  496. preferredStyle:UIAlertControllerStyleActionSheet];
  497. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  498. actionSheet.popoverPresentationController.sourceView = self.segmentedControl;
  499. actionSheet.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMaxX(self.segmentedControl.bounds), CGRectGetMaxY(self.segmentedControl.bounds), 1, 1);
  500. }
  501. [actionSheet addAction:cameraAction];
  502. [actionSheet addAction:photoAction];
  503. [actionSheet addAction:cancelAction];
  504. actionSheet.modalPresentationStyle = UIModalPresentationPopover;
  505. [self presentViewController:actionSheet animated:YES completion:nil];
  506. }
  507. #pragma mark - UIImagePickerControllerDelegate
  508. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  509. [picker dismissViewControllerAnimated:YES completion:nil];
  510. UIImage *image;
  511. if ([info objectForKey:UIImagePickerControllerEditedImage]) {
  512. image = [info objectForKey:UIImagePickerControllerEditedImage];
  513. } else if ([info objectForKey:UIImagePickerControllerOriginalImage]) {
  514. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  515. }
  516. if (image) {
  517. self.isImageSignature = YES;
  518. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  519. self.saveButton.enabled = YES;
  520. }
  521. UIImageOrientation imageOrientation = image.imageOrientation;
  522. if (imageOrientation!=UIImageOrientationUp) {
  523. UIGraphicsBeginImageContext(image.size);
  524. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  525. image = UIGraphicsGetImageFromCurrentImageContext();
  526. UIGraphicsEndImageContext();
  527. }
  528. NSData *imageData = UIImagePNGRepresentation(image);
  529. if (imageData == nil || [imageData length] <= 0) {
  530. return;
  531. }
  532. image = [UIImage imageWithData:imageData];
  533. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  534. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  535. if (imageRef) {
  536. image = [UIImage imageWithCGImage:imageRef];
  537. CGImageRelease(imageRef);
  538. }
  539. self.signatureDrawImageView.image = image;
  540. [self.signatureDrawImageView setNeedsDisplay];
  541. }
  542. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  543. [picker dismissViewControllerAnimated:YES completion:nil];
  544. }
  545. #pragma mark - CPDFColorSelectViewDelegate
  546. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  547. self.textField.textColor = color;
  548. self.signatureDrawTextView.color = color;
  549. [self.signatureDrawTextView setNeedsDisplay];
  550. }
  551. - (void)selectColorView:(CPDFColorSelectView *)select {
  552. if (@available(iOS 14.0, *)) {
  553. UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
  554. picker.delegate = self;
  555. [self presentViewController:picker animated:YES completion:nil];
  556. } else {
  557. UIDevice *currentDevice = [UIDevice currentDevice];
  558. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  559. // This is an iPad
  560. _colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 520)];
  561. } else {
  562. // This is an iPhone or iPod touch
  563. _colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 320)];
  564. }
  565. self.colorPicker.delegate = self;
  566. self.colorPicker.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  567. [self.view addSubview:self.colorPicker];
  568. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  569. }
  570. }
  571. #pragma mark - CPDFColorPickerViewDelegate
  572. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  573. self.signatureDrawTextView.color = color;
  574. self.textField.textColor = self.signatureDrawTextView.color;
  575. [self.signatureDrawImageView setNeedsDisplay];
  576. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  577. }
  578. #pragma mark - UIColorPickerViewControllerDelegate
  579. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  580. self.signatureDrawTextView.color = viewController.selectedColor;
  581. self.textField.textColor = self.signatureDrawTextView.color;
  582. [self.signatureDrawTextView setNeedsDisplay];
  583. }
  584. #pragma mark - CSignatureDrawViewDelegate
  585. - (void)signatureDrawViewStart:(CSignatureDrawView *)signatureDrawView {
  586. [self.saveButton setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  587. self.saveButton.enabled = YES;
  588. self.isDrawSignature = YES;
  589. self.emptyLabel.text = @"";
  590. }
  591. - (void)appWillResignActive:(NSNotification *)notification {
  592. [self.textField resignFirstResponder];
  593. }
  594. @end