// // KMVerificationTrialViewController.m // PDF Reader Pro Edition // // Created by 王帅 on 2018/5/8. // #import "KMVerificationTrialViewController.h" #import @interface KMVerificationTrialViewController () @property (nonatomic,assign) IBOutlet NSTextField *label; @property (nonatomic,assign) IBOutlet NSTextField *sublabel; @property (nonatomic,assign) IBOutlet NSView *emailTextFieldView; @property (nonatomic,assign) IBOutlet NSTextField *emailTextField; @property (nonatomic,assign) IBOutlet NSTextField *emailInvalidLabel; @property (nonatomic,assign) IBOutlet NSView *nameTextFieldView; @property (nonatomic,assign) IBOutlet NSTextField *nameTextField; @property (nonatomic,assign) IBOutlet NSTextField *nameInvalidLabel; @property (nonatomic,assign) IBOutlet KMCustomButton *licenseButton; @property (nonatomic,assign) IBOutlet KMCustomButton *continueButton; @property(nonatomic, assign) IBOutlet NSTextView *accoutTextView; @property (assign) IBOutlet NSLayoutConstraint *textViewHeight; @property (nonatomic, retain) CALayer *continueButtonLayer; @end @implementation KMVerificationTrialViewController #pragma mark Init Methods - (instancetype)init { if (self = [super initWithNibName:@"KMVerificationTrialViewController" bundle:nil]) { } return self; } - (void)dealloc { } #pragma mark View Methods - (void)loadView { [super loadView]; // Do view setup here. self.emailTextFieldView.wantsLayer = YES; self.emailTextFieldView.layer.masksToBounds = YES; self.emailTextFieldView.layer.cornerRadius = 4.0; self.emailTextFieldView.layer.borderWidth = 1.0; self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; self.nameTextFieldView.wantsLayer = YES; self.nameTextFieldView.layer.masksToBounds = YES; self.nameTextFieldView.layer.cornerRadius = 2.0; self.nameTextFieldView.layer.borderWidth = 1.0; self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; self.licenseButton.wantsLayer = YES; self.licenseButton.layer.masksToBounds = YES; self.licenseButton.layer.cornerRadius = 1.0; self.continueButton.wantsLayer = YES; self.continueButton.layer.masksToBounds = YES; self.continueButton.layer.cornerRadius = 1.0; self.label.stringValue = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Welcome to", nil), @"LynxPDF Editor"]; self.sublabel.stringValue = NSLocalizedString(@"Enter your email to activate 7-Day Trial", nil); [self.emailTextField.cell setPlaceholderString:NSLocalizedString(@"Enter your email", nil)]; self.emailInvalidLabel.stringValue = NSLocalizedString(@"Please enter valid email", nil); [self.nameTextField.cell setPlaceholderString:NSLocalizedString(@"Enter your name", nil)]; self.nameInvalidLabel.stringValue = NSLocalizedString(@"Please enter valid username", nil); self.licenseButton.title = NSLocalizedString(@"Enter License Number", nil); self.continueButton.title = NSLocalizedString(@"Activate", nil); NSString * str1 = NSLocalizedString(@"Privacy Policy",nil); NSString * str = [NSString stringWithFormat:NSLocalizedString(@"By clicking the buttons below you confirm that you have read our %@.", nil),str1]; NSRange range1 = [str rangeOfString:str1]; NSFont * font = [NSFont systemFontOfSize:11.0]; NSMutableAttributedString *mastring = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:font}]; CGSize size = [self sizeOfString:str witFontSize:font]; self.textViewHeight.constant = size.height; [mastring addAttribute:NSForegroundColorAttributeName value:[NSColor textColor] range:NSMakeRange(0, str.length)]; [mastring addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:0 green:122.0/255.0 blue:1.0 alpha:1.0] range:range1]; NSString *valueString1 = [[NSString stringWithFormat:@"%@",@"https://www.pdfreaderpro.com/privacy-policy"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; [mastring addAttribute:NSLinkAttributeName value:valueString1 range:range1]; [self.accoutTextView.textStorage appendAttributedString:mastring]; [self.accoutTextView setAlignment:NSTextAlignmentCenter]; self.continueButtonLayer = [CALayer layer]; [self.continueButton.layer addSublayer:self.continueButtonLayer]; self.continueButtonLayer.frame = CGRectMake(0, 0, CGRectGetWidth(_continueButton.bounds), CGRectGetHeight(_continueButton.bounds)); self.continueButtonLayer.backgroundColor = [KMAppearance KMColor_Status_Sel].CGColor; self.continueButtonLayer.cornerRadius = 0; self.continueButtonLayer.hidden = YES; __block KMVerificationTrialViewController *blockSelf = self; self.continueButton.mouseMoveCallback = ^(BOOL mouseEntered) { if (mouseEntered) blockSelf.continueButtonLayer.hidden = NO; else blockSelf.continueButtonLayer.hidden = YES; }; self.licenseButton.mouseMoveCallback = nil; [self.label setTextColor:[KMAppearance KMColor_Layout_H0]]; [self.sublabel setTextColor:[KMAppearance KM_242424_Color75]]; [self.licenseButton setTitleColor:[KMAppearance KMColor_Layout_H0]]; [self.continueButton setTitleColor:[NSColor whiteColor]]; [self.emailTextField setTextColor:[KMAppearance KMColor_Layout_H0]]; [self.nameTextField setTextColor:[KMAppearance KMColor_Layout_H0]]; } - (NSSize)sizeOfString:(NSString *)string witFontSize:(NSFont *)font { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:font forKey:NSFontAttributeName]; CGSize size = [string boundingRectWithSize:CGSizeMake(self.accoutTextView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictionary].size; return size; } - (void)viewDidAppear { [super viewDidAppear]; [self.nameTextField becomeFirstResponder]; } #pragma mark Button Actions - (IBAction)licenseButtonAction:(id)sender { if (self.callback) { self.callback(0, self.emailTextField.stringValue, self.nameTextField.stringValue); } } - (IBAction)continueButtonAction:(id)sender { BOOL isValidate = YES; if ([self isValidateEmail:self.emailTextField.stringValue]) { self.emailInvalidLabel.hidden = YES; self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; } else { self.emailInvalidLabel.hidden = NO; self.emailTextFieldView.layer.borderColor = self.emailInvalidLabel.textColor.CGColor; isValidate = NO; } if (self.nameTextField.stringValue.length > 0) { self.nameInvalidLabel.hidden = YES; self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; } else { self.nameInvalidLabel.hidden = NO; self.nameTextFieldView.layer.borderColor = self.nameInvalidLabel.textColor.CGColor; isValidate = NO; } if (!isValidate) { return; } if (self.callback) { self.callback(1, self.emailTextField.stringValue, self.nameTextField.stringValue); } } - (void)setEnabled:(BOOL)enabled { self.licenseButton.enabled = enabled; self.continueButton.enabled = enabled; self.emailTextField.enabled = enabled; self.nameTextField.enabled = enabled; } #pragma mark NSTextFieldDelegate - (void)controlTextDidChange:(NSNotification *)notification { NSTextField *textField = notification.object; if (textField == self.emailTextField) { self.emailInvalidLabel.hidden = YES; self.emailTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; } else if (textField == self.nameTextField) { self.nameInvalidLabel.hidden = YES; self.nameTextFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor; } } #pragma mark Private Methods - (BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex]; return [emailTest evaluateWithObject:email]; } @end