KMVerificationTrialViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // KMVerificationTrialViewController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 王帅 on 2018/5/8.
  6. //
  7. #import "KMVerificationTrialViewController.h"
  8. #import <PDF_Reader_Pro-Swift.h>
  9. #import "KMOCLanguage.h"
  10. @interface KMVerificationTrialViewController ()
  11. @property (nonatomic,assign) IBOutlet NSTextField *label;
  12. @property (nonatomic,assign) IBOutlet NSTextField *sublabel;
  13. @property (nonatomic,assign) IBOutlet NSView *emailTextFieldView;
  14. @property (nonatomic,assign) IBOutlet NSTextField *emailTextField;
  15. @property (nonatomic,assign) IBOutlet NSTextField *emailInvalidLabel;
  16. @property (nonatomic,assign) IBOutlet NSView *nameTextFieldView;
  17. @property (nonatomic,assign) IBOutlet NSTextField *nameTextField;
  18. @property (nonatomic,assign) IBOutlet NSTextField *nameInvalidLabel;
  19. @property (nonatomic,assign) IBOutlet KMCustomButton *licenseButton;
  20. @property (nonatomic,assign) IBOutlet KMCustomButton *continueButton;
  21. @property(nonatomic, assign) IBOutlet NSTextView *accoutTextView;
  22. @property (assign) IBOutlet NSLayoutConstraint *textViewHeight;
  23. @property (nonatomic, retain) CALayer *continueButtonLayer;
  24. @end
  25. @implementation KMVerificationTrialViewController
  26. #pragma mark Init Methods
  27. - (instancetype)init {
  28. if (self = [super initWithNibName:@"KMVerificationTrialViewController" bundle:nil]) {
  29. }
  30. return self;
  31. }
  32. - (void)dealloc {
  33. }
  34. #pragma mark View Methods
  35. - (void)loadView {
  36. [super loadView];
  37. // Do view setup here.
  38. self.emailTextFieldView.wantsLayer = YES;
  39. self.emailTextFieldView.layer.masksToBounds = YES;
  40. self.emailTextFieldView.layer.cornerRadius = 4.0;
  41. self.emailTextFieldView.layer.borderWidth = 1.0;
  42. self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  43. self.nameTextFieldView.wantsLayer = YES;
  44. self.nameTextFieldView.layer.masksToBounds = YES;
  45. self.nameTextFieldView.layer.cornerRadius = 2.0;
  46. self.nameTextFieldView.layer.borderWidth = 1.0;
  47. self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  48. self.licenseButton.wantsLayer = YES;
  49. self.licenseButton.layer.masksToBounds = YES;
  50. self.licenseButton.layer.cornerRadius = 1.0;
  51. self.continueButton.wantsLayer = YES;
  52. self.continueButton.layer.masksToBounds = YES;
  53. self.continueButton.layer.cornerRadius = 1.0;
  54. self.label.stringValue = [NSString stringWithFormat:@"%@ %@", KMLocalizedString(@"Welcome to", nil), @"PDF Reader Pro"];
  55. self.sublabel.stringValue = KMLocalizedString(@"Enter your email to activate 7-Day Trial", nil);
  56. [self.emailTextField.cell setPlaceholderString:KMLocalizedString(@"Enter your email", nil)];
  57. self.emailInvalidLabel.stringValue = KMLocalizedString(@"Please enter valid email", nil);
  58. [self.nameTextField.cell setPlaceholderString:KMLocalizedString(@"Enter your name", nil)];
  59. self.nameInvalidLabel.stringValue = KMLocalizedString(@"Please enter valid username", nil);
  60. self.licenseButton.title = KMLocalizedString(@"Enter License Number", nil);
  61. self.continueButton.title = KMLocalizedString(@"Activate", nil);
  62. NSString * str1 = KMLocalizedString(@"Privacy Policy",nil);
  63. NSString * str = [NSString stringWithFormat:KMLocalizedString(@"By clicking the buttons below you confirm that you have read our %@.", nil),str1];
  64. NSRange range1 = [str rangeOfString:str1];
  65. NSFont * font = [NSFont systemFontOfSize:11.0];
  66. NSMutableAttributedString *mastring = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:font}];
  67. CGSize size = [self sizeOfString:str witFontSize:font];
  68. self.textViewHeight.constant = size.height;
  69. [mastring addAttribute:NSForegroundColorAttributeName value:[NSColor textColor] range:NSMakeRange(0, str.length)];
  70. [mastring addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:0 green:122.0/255.0 blue:1.0 alpha:1.0] range:range1];
  71. NSString *valueString1 = [[NSString stringWithFormat:@"%@",@"https://www.pdfreaderpro.com/privacy-policy"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  72. [mastring addAttribute:NSLinkAttributeName value:valueString1 range:range1];
  73. [self.accoutTextView.textStorage appendAttributedString:mastring];
  74. [self.accoutTextView setAlignment:NSTextAlignmentCenter];
  75. self.continueButtonLayer = [CALayer layer];
  76. [self.continueButton.layer addSublayer:self.continueButtonLayer];
  77. self.continueButtonLayer.frame = CGRectMake(0, 0, CGRectGetWidth(_continueButton.bounds), CGRectGetHeight(_continueButton.bounds));
  78. self.continueButtonLayer.backgroundColor = [KMAppearance KMColor_Status_Sel].CGColor;
  79. self.continueButtonLayer.cornerRadius = 0;
  80. self.continueButtonLayer.hidden = YES;
  81. __block KMVerificationTrialViewController *blockSelf = self;
  82. self.continueButton.mouseMoveCallback = ^(BOOL mouseEntered) {
  83. if (mouseEntered)
  84. blockSelf.continueButtonLayer.hidden = NO;
  85. else
  86. blockSelf.continueButtonLayer.hidden = YES;
  87. };
  88. self.licenseButton.mouseMoveCallback = nil;
  89. [self.label setTextColor:[KMAppearance KMColor_Layout_H0]];
  90. [self.sublabel setTextColor:[KMAppearance KM_242424_Color75]];
  91. [self.licenseButton setTitleColor:[KMAppearance KMColor_Layout_H0]];
  92. [self.continueButton setTitleColor:[NSColor whiteColor]];
  93. [self.emailTextField setTextColor:[KMAppearance KMColor_Layout_H0]];
  94. [self.nameTextField setTextColor:[KMAppearance KMColor_Layout_H0]];
  95. }
  96. - (NSSize)sizeOfString:(NSString *)string witFontSize:(NSFont *)font
  97. {
  98. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  99. [dictionary setObject:font forKey:NSFontAttributeName];
  100. CGSize size = [string boundingRectWithSize:CGSizeMake(self.accoutTextView.frame.size.width, MAXFLOAT)
  101. options:NSStringDrawingUsesLineFragmentOrigin
  102. attributes:dictionary].size;
  103. return size;
  104. }
  105. - (void)viewDidAppear {
  106. [super viewDidAppear];
  107. [self.nameTextField becomeFirstResponder];
  108. }
  109. #pragma mark Button Actions
  110. - (IBAction)licenseButtonAction:(id)sender {
  111. if (self.callback) {
  112. self.callback(0, self.emailTextField.stringValue, self.nameTextField.stringValue);
  113. }
  114. }
  115. - (IBAction)continueButtonAction:(id)sender {
  116. BOOL isValidate = YES;
  117. if ([self isValidateEmail:self.emailTextField.stringValue]) {
  118. self.emailInvalidLabel.hidden = YES;
  119. self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  120. } else {
  121. self.emailInvalidLabel.hidden = NO;
  122. self.emailTextFieldView.layer.borderColor = self.emailInvalidLabel.textColor.CGColor;
  123. isValidate = NO;
  124. }
  125. if (self.nameTextField.stringValue.length > 0) {
  126. self.nameInvalidLabel.hidden = YES;
  127. self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  128. } else {
  129. self.nameInvalidLabel.hidden = NO;
  130. self.nameTextFieldView.layer.borderColor = self.nameInvalidLabel.textColor.CGColor;
  131. isValidate = NO;
  132. }
  133. if (!isValidate) {
  134. return;
  135. }
  136. if (self.callback) {
  137. self.callback(1, self.emailTextField.stringValue, self.nameTextField.stringValue);
  138. }
  139. }
  140. - (void)setEnabled:(BOOL)enabled {
  141. self.licenseButton.enabled = enabled;
  142. self.continueButton.enabled = enabled;
  143. self.emailTextField.enabled = enabled;
  144. self.nameTextField.enabled = enabled;
  145. }
  146. #pragma mark NSTextFieldDelegate
  147. - (void)controlTextDidChange:(NSNotification *)notification {
  148. NSTextField *textField = notification.object;
  149. if (textField == self.emailTextField) {
  150. self.emailInvalidLabel.hidden = YES;
  151. self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  152. } else if (textField == self.nameTextField) {
  153. self.nameInvalidLabel.hidden = YES;
  154. self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
  155. }
  156. }
  157. #pragma mark Private Methods
  158. - (BOOL)isValidateEmail:(NSString *)email {
  159. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  160. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
  161. return [emailTest evaluateWithObject:email];
  162. }
  163. @end