KMVerificationTrialViewController.m 8.1 KB

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