KMPDFSignatureTextView.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // KMPDFSignatureTextView.m
  3. // PDF Reader
  4. //
  5. // Created by 丁林圭 on 2018/6/8.
  6. // Copyright © 2018年 Kdan Mobile. All rights reserved.
  7. //
  8. #import "KMPDFSignatureTextView.h"
  9. #import "NSImage+CustomImage.h"
  10. #define kKMSignayureTextMaxFontSize 28.0
  11. #define kKMSignayureTextMaxWidth 300
  12. #define kKMSignayureTextEdgeOffset 5
  13. #pragma mark KMVerticallyCenteredSecureTextFieldCell
  14. @interface KMVerticallyCenteredSecureTextFieldCell : NSTextFieldCell
  15. @end
  16. @implementation KMVerticallyCenteredSecureTextFieldCell
  17. - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame
  18. {
  19. // NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender])));
  20. NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender])));
  21. return NSInsetRect(frame, 0.0, offset);
  22. }
  23. - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event
  24. {
  25. [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate event:event];
  26. }
  27. - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate start:(NSInteger)start length:(NSInteger)length
  28. {
  29. [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate start:start length:length];
  30. }
  31. - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view
  32. {
  33. [super drawInteriorWithFrame:
  34. [self adjustedFrameToVerticallyCenterText:frame] inView:view];
  35. }
  36. @end
  37. @interface KMSignayureTextField : NSTextField
  38. @property (nonatomic, retain) NSColor *originalTextColor;
  39. @end
  40. @implementation KMSignayureTextField
  41. - (void)addSubview:(NSView *)view {
  42. [super addSubview:view];
  43. NSView * view1 = view.subviews.firstObject;
  44. if ([view1 isKindOfClass:[NSTextView class]]) {
  45. NSTextView * textview = (NSTextView *)view1;
  46. [textview setInsertionPointColor:[NSColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0]];
  47. }
  48. }
  49. @end
  50. @interface KMPDFSignatureTextView()
  51. @property (nonatomic) IBOutlet KMSignayureTextField *nameTextFiled;
  52. @property (assign) IBOutlet NSLayoutConstraint *labelWidthLayoutConstraint;
  53. @property (nonatomic) IBOutlet NSBox *verticalLineBox;
  54. @end
  55. @implementation KMPDFSignatureTextView
  56. - (id)initWithFrame:(NSRect)frameRect
  57. {
  58. if (self = [super initWithFrame:frameRect])
  59. {
  60. self.keyboardColor = [NSColor colorWithDeviceRed:0 green:0 blue:0 alpha:1];
  61. self.wantsLayer = YES;
  62. self.layer.borderWidth = 1.0;
  63. self.layer.borderColor =[NSColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.05].CGColor;
  64. self.layer.backgroundColor = [NSColor colorWithRed:1 green:1 blue:1 alpha:0.9].CGColor;
  65. }
  66. return self;
  67. }
  68. -(void)awakeFromNib
  69. {
  70. [super awakeFromNib];
  71. // NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
  72. // paragraphStyle.alignment = NSTextAlignmentCenter;
  73. //
  74. // NSAttributedString *att = [[[NSAttributedString alloc] initWithString:NSLocalizedString(@"Sign Here",nil) attributes:@{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H2],NSFontAttributeName:[NSFont systemFontOfSize:14.0], NSParagraphStyleAttributeName:paragraphStyle}] autorelease];
  75. //
  76. // [self.nameTextFiled.cell setPlaceholderAttributedString:att];
  77. self.nameTextFiled.placeholderString = NSLocalizedString(@"Sign Here",nil);
  78. self.nameTextFiled.font = [NSFont systemFontOfSize:14.0];
  79. [self.verticalLineBox setFillColor:[NSColor blackColor]];
  80. }
  81. #pragma mark - Public Method
  82. - (void)clearImage
  83. {
  84. self.nameTextFiled.stringValue = @"";
  85. self.nameTextFiled.font = [NSFont systemFontOfSize:14.0];
  86. if (self.changeSignatureTextCallback) {
  87. self.changeSignatureTextCallback(NO);
  88. }
  89. }
  90. - (NSImage *)signatureImage
  91. {
  92. CGRect rect = CGRectZero;
  93. NSString * string = self.nameTextFiled.stringValue?:@"";
  94. CGSize size = [self sizeOfString:string witFontSize:self.nameTextFiled.font];
  95. if (size.width == 0) {
  96. return nil;
  97. } else {
  98. rect = CGRectMake(kKMSignayureTextEdgeOffset,kKMSignayureTextEdgeOffset, size.width, size.height);
  99. }
  100. NSImage *image = [[NSImage alloc] initWithSize:CGSizeMake(size.width + kKMSignayureTextEdgeOffset* 2, size.height + kKMSignayureTextEdgeOffset* 2)];
  101. [image lockFocus];
  102. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  103. [dictionary setObject:self.nameTextFiled.font forKey:NSFontAttributeName];
  104. [dictionary setObject:self.keyboardColor forKey:NSForegroundColorAttributeName];
  105. [string drawInRect:rect withAttributes:dictionary];
  106. [image unlockFocus];
  107. return image;
  108. }
  109. - (void)setKeyboardColor:(NSColor *)keyboardColor
  110. {
  111. _keyboardColor = keyboardColor;
  112. self.nameTextFiled.textColor = keyboardColor;
  113. }
  114. - (void)setFontName:(NSString *)fontName
  115. {
  116. _fontName = fontName;
  117. CGFloat fontSize = 14.0;
  118. if (self.nameTextFiled.stringValue.length > 0) {
  119. fontSize = kKMSignayureTextMaxFontSize;
  120. }
  121. NSFont * maxFont = [NSFont fontWithName:_fontName size:fontSize];
  122. CGSize size = [self sizeOfString:self.nameTextFiled.stringValue witFontSize:maxFont];
  123. CGFloat width = size.width + kKMSignayureTextEdgeOffset * 2;
  124. CGFloat scanel = 1.0;
  125. scanel = MIN(kKMSignayureTextMaxWidth/width, 60.0/(size.height + 2));
  126. NSFont* font = [NSFont fontWithName:_fontName size:fontSize];
  127. self.nameTextFiled.font = font;
  128. }
  129. #pragma mark - NSTextFieldDelegate
  130. - (void)controlTextDidChange:(NSNotification *)obj
  131. {
  132. NSTextField *textField = (NSTextField*)[obj object];
  133. if (textField == self.nameTextFiled) {
  134. NSString *string = textField.stringValue.length > 0 ? textField.stringValue : nil;
  135. CGFloat fontSize = 14.0;
  136. if (string) {
  137. fontSize = kKMSignayureTextMaxFontSize;
  138. }
  139. NSFont * maxFont = [NSFont fontWithName:_fontName size:fontSize];
  140. CGSize size = [self sizeOfString:string witFontSize:maxFont];
  141. CGFloat width = size.width + kKMSignayureTextEdgeOffset * 2;
  142. CGFloat scanel = 1.0;
  143. scanel = MIN((self.bounds.size.width - 30)/width, 60.0/(size.height + 2));
  144. if (width >= 100) {
  145. self.labelWidthLayoutConstraint.constant = width;
  146. }
  147. NSFont* font = [NSFont fontWithName:_fontName size:fontSize];
  148. self.nameTextFiled.font = font;
  149. BOOL isTure = textField.stringValue.length > 0 ? YES : NO;
  150. if (self.changeSignatureTextCallback) {
  151. self.changeSignatureTextCallback(isTure);
  152. }
  153. if([self.delegate respondsToSelector:@selector(changeSignatureText:)]) {
  154. [self.delegate changeSignatureText:isTure];
  155. }
  156. }
  157. }
  158. - (NSSize)sizeOfString:(NSString *)string witFontSize:(NSFont *)font
  159. {
  160. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  161. [dictionary setObject:font forKey:NSFontAttributeName];
  162. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  163. [style setAlignment:NSCenterTextAlignment];
  164. [dictionary setObject:style forKey:NSParagraphStyleAttributeName];
  165. CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
  166. options:NSStringDrawingUsesLineFragmentOrigin
  167. attributes:dictionary].size;
  168. return size;
  169. }
  170. @end