|
//
// registerController.m
// ThePaperBase
//
// Created by Huixin on 15/8/19.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "registerController.h"
#import "agreementController.h"
#import "userSystemViewModel.h"
@interface registerController () <UITextFieldDelegate, userSystemViewModel>
@property(nonatomic, strong)UIImageView *userImageView;
@property(nonatomic, strong)UITextField *userField;
@property(nonatomic, strong)UIView *userLine;
@property(nonatomic, strong)UILabel *userLabel;
@property(nonatomic, strong)UIImageView *passwordImageView;
@property(nonatomic, strong)UITextField *passwordField;
@property(nonatomic, strong)UIView *passwordLine;
@property(nonatomic, strong)UILabel *passwordLabel;
@property(nonatomic, strong)UIButton *selectBtn;
@property(nonatomic, strong)UIButton *readRegisterContentBtn;
@property(nonatomic, strong)TPCustomButton *confirmBtn;
@property(nonatomic, strong)userSystemViewModel *viewModel;
@end
@implementation registerController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.titleStr = @"注册";
_viewModel = [userSystemViewModel new];
_viewModel.delegate = self;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tapGesture];
[self.view addSubview:self.userImageView];
[self.view addSubview:self.userField];
[self.view addSubview:self.userLine];
[self.view addSubview:self.userLabel];
[self.view addSubview:self.passwordImageView];
[self.view addSubview:self.passwordField];
[self.view addSubview:self.passwordLine];
[self.view addSubview:self.passwordLabel];
[self.view addSubview:self.selectBtn];
[self.view addSubview:self.readRegisterContentBtn];
[self.view addSubview:self.confirmBtn];
[self.userField becomeFirstResponder];
[self layoutViews];
}
- (void)layoutViews {
[self.userImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(20);
make.bottom.equalTo(self.userField.bottom);
make.width.mas_equalTo(@20);
make.height.mas_equalTo(@19);
}];
[self.userField makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom).offset(38);
make.left.equalTo(self.view.left).offset(50);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@19);
}];
[self.userLine makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.userField.bottom).offset(7);
make.left.equalTo(self.userImageView.left);
make.right.equalTo(self.userField.right);
make.height.mas_equalTo(@1);
}];
[self.userLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.userLine.bottom).offset(4);
make.left.equalTo(self.userLine.left);
make.right.equalTo(self.userLine.right);
make.height.mas_equalTo(12);
}];
[self.passwordImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(20);
make.bottom.equalTo(self.passwordField.bottom);
make.width.mas_equalTo(@18);
make.height.mas_equalTo(@20);
}];
[self.passwordField makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.userLabel.bottom).offset(22);
make.left.equalTo(self.view.left).offset(50);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@19);
}];
[self.passwordLine makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.passwordField.bottom).offset(7);
make.left.equalTo(self.passwordImageView.left);
make.right.equalTo(self.passwordField.right);
make.height.mas_equalTo(@1);
}];
[self.passwordLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.passwordLine.bottom).offset(4);
make.left.equalTo(self.passwordLine.left);
make.right.equalTo(self.passwordLine.right);
make.height.mas_equalTo(12);
}];
[self.selectBtn makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.readRegisterContentBtn.centerY);
make.left.equalTo(self.view.left).offset(20);
make.width.and.height.mas_equalTo(@15);
}];
[self.readRegisterContentBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.passwordLabel.bottom).offset(15);
make.left.equalTo(self.selectBtn.right);
make.width.mas_equalTo(@100);
make.height.mas_equalTo(@15);
}];
[self.confirmBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.readRegisterContentBtn.bottom).offset(37);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.confirmBtn.width).multipliedBy(0.15);
}];
}
#pragma mark - get method
- (UIImageView*)userImageView {
if (!_userImageView) {
_userImageView = [[UIImageView alloc] init];
_userImageView.image = Image(@"login/userName.png");
}
return _userImageView;
}
- (UITextField*)userField {
if (!_userField) {
_userField = [[UITextField alloc] init];
_userField.backgroundColor = [UIColor clearColor];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}];
_userField.attributedPlaceholder = atrString;
_userField.text = [TPUserDefault instance].registerBO.sname?[TPUserDefault instance].registerBO.sname:@"";
_userField.textColor = [UIColor colorWithHexString:TextBlack];
_userField.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_userField.textAlignment = NSTextAlignmentLeft;
_userField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_userField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_userField.keyboardType = UIKeyboardTypeDefault;
_userField.clearButtonMode = UITextFieldViewModeWhileEditing;
_userField.delegate = self;
[_userField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _userField;
}
- (UIView*)userLine {
if (!_userLine) {
_userLine = [[UIView alloc] init];
_userLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _userLine;
}
- (UILabel*)userLabel {
if (!_userLabel) {
_userLabel = [[UILabel alloc] init];
_userLabel.backgroundColor = [UIColor clearColor];
_userLabel.text = userNameRangeHolder;
_userLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_userLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
_userLabel.textAlignment = NSTextAlignmentRight;
}
return _userLabel;
}
- (UIImageView*)passwordImageView {
if (!_passwordImageView) {
_passwordImageView = [[UIImageView alloc] init];
_passwordImageView.image = Image(@"login/password.png");
}
return _passwordImageView;
}
- (UITextField*)passwordField {
if (!_passwordField) {
_passwordField = [[UITextField alloc] init];
_passwordField.backgroundColor = [UIColor clearColor];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}];
_passwordField.attributedPlaceholder = atrString;
_passwordField.text = [TPUserDefault instance].registerBO.pwd?[TPUserDefault instance].registerBO.pwd:@"";
_passwordField.textColor = [UIColor colorWithHexString:TextBlack];
_passwordField.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_passwordField.textAlignment = NSTextAlignmentLeft;
_passwordField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_passwordField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_passwordField.keyboardType = UIKeyboardTypeASCIICapable;
_passwordField.secureTextEntry = YES;
_passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
_passwordField.delegate = self;
[_passwordField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
_passwordField.returnKeyType = UIReturnKeyDone; //bug:5035 关于密码的地方,键盘都改成“确定”
}
return _passwordField;
}
- (UIView*)passwordLine {
if (!_passwordLine) {
_passwordLine = [[UIView alloc] init];
_passwordLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _passwordLine;
}
- (UILabel*)passwordLabel {
if (!_passwordLabel) {
_passwordLabel = [[UILabel alloc] init];
_passwordLabel.backgroundColor = [UIColor clearColor];
_passwordLabel.text = passwordRangeHolder;
_passwordLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_passwordLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
_passwordLabel.textAlignment = NSTextAlignmentRight;
}
return _passwordLabel;
}
- (UIButton*)selectBtn {
if (!_selectBtn) {
_selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_selectBtn setImage:Image(@"login/selectedIcon.png") forState:UIControlStateSelected];
[_selectBtn setImage:Image(@"login/unselectedIcon.png") forState:UIControlStateNormal];
_selectBtn.backgroundColor = [UIColor clearColor];
[_selectBtn addTarget:self action:@selector(selectRegister:) forControlEvents:UIControlEventTouchUpInside];
[_selectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
_selectBtn.selected = YES;
}
return _selectBtn;
}
- (UIButton*)readRegisterContentBtn {
if (!_readRegisterContentBtn) {
_readRegisterContentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_readRegisterContentBtn.backgroundColor = [UIColor clearColor];
[_readRegisterContentBtn setTitle:@"我已阅读相关协议" forState:UIControlStateNormal];
[_readRegisterContentBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
[_readRegisterContentBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
_readRegisterContentBtn.titleLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
[_readRegisterContentBtn addTarget:self action:@selector(readContent:) forControlEvents:UIControlEventTouchUpInside];
}
return _readRegisterContentBtn;
}
- (TPCustomButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [[TPCustomButton alloc] init];
_confirmBtn.title = @"完成注册";
[_confirmBtn addTarget:self action:@selector(confirmEvent) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
#pragma mark - textfield delegate
- (void)textFieldDidChange:(UITextField *)textField {
if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) {
textField.text = disable_emoji(textField.text);
}
if (textField == self.userField) {
[TPUserDefault instance].registerBO.sname = textField.text;
}else if (textField == self.passwordField) {
[TPUserDefault instance].registerBO.pwd = textField.text;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == _userField) {
[_passwordField becomeFirstResponder];
}
else if (textField == _passwordField) {
[self confirmEvent];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isFirstResponder]) {
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
return NO;
}
}
return YES;
}
#pragma mark - btn event handler
- (void)selectRegister:(UIButton*)btn {
btn.selected = !btn.selected;
}
- (void)readContent:(UIButton*)btn {
agreementController *vc = agreementController.new;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)confirmEvent {
[self closeKeyBoard];
[_viewModel confirmRegister:_selectBtn];
}
#pragma mark - viewmodel delegate
- (void)registerSuccess {
ShowTextMessage(@"注册成功");
[[NSNotificationCenter defaultCenter] postNotificationName:HAVELOGIN object:nil];
}
#pragma mark - tap handler
- (void)tap:(UITapGestureRecognizer*)sender {
[self closeKeyBoard];
}
#pragma mark - keyBoard event
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|