// // editSnameController.m // ThePaperDemo // // Created by scar1900 on 14/12/15. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "editSnameController.h" @interface editSnameController () @property(nonatomic, strong)UITextField *snameField; @property(nonatomic, strong)UIView *lineView; @property(nonatomic, strong)UILabel *textRangeLabel; @property(nonatomic, strong)TPCustomButton *confirmButton; @end @implementation editSnameController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.titleStr =@"修改昵称"; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [self.view addGestureRecognizer:tapGesture]; [self.view addSubview:self.snameField]; [self.view addSubview:self.lineView]; [self.view addSubview:self.textRangeLabel]; [self.view addSubview:self.confirmButton]; [self.snameField becomeFirstResponder]; [self layoutViews]; } - (void)layoutViews { [self.snameField makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom).offset(36); make.left.equalTo(self.view.left).offset(20); make.right.equalTo(self.view.right).offset(-20); make.height.mas_equalTo(@19); }]; [self.lineView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.snameField.bottom).offset(6); make.left.equalTo(self.snameField.left); make.right.equalTo(self.snameField.right); make.height.mas_equalTo(@1); }]; [self.textRangeLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.lineView.bottom).offset(4); make.left.equalTo(self.snameField.left); make.right.equalTo(self.snameField.right); make.height.mas_equalTo(@12); }]; [self.confirmButton makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.textRangeLabel.bottom).offset(45); make.left.equalTo(self.snameField.left); make.right.equalTo(self.snameField.right); make.height.equalTo(self.confirmButton.width).multipliedBy(0.15); }]; } - (UITextField*)snameField { if (!_snameField) { _snameField = [[UITextField alloc] init]; _snameField.backgroundColor = [UIColor clearColor]; _snameField.textAlignment = NSTextAlignmentLeft; _snameField.textColor = [UIColor colorWithHexString:TextBlack]; _snameField.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _snameField.keyboardAppearance = UIKeyboardAppearanceDark; } _snameField.keyboardType = UIKeyboardTypeDefault; _snameField.clearButtonMode = UITextFieldViewModeWhileEditing; NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入昵称" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}]; _snameField.attributedPlaceholder = atrString; _snameField.delegate = self; [_snameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; _snameField.returnKeyType = UIReturnKeyDone; } _snameField.font = appFont(TEXT_FOUR_LEVELSIZE, NO); return _snameField; } - (UIView*)lineView { if (!_lineView) { _lineView = [[UIView alloc] init]; _lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _lineView; } - (UILabel*)textRangeLabel { if(!_textRangeLabel) { _textRangeLabel = [[UILabel alloc] init]; _textRangeLabel.backgroundColor = [UIColor clearColor]; _textRangeLabel.textAlignment = NSTextAlignmentRight; _textRangeLabel.textColor = [UIColor colorWithHexString:TextLightGray]; _textRangeLabel.text = @"请输入4-20个字符"; } _textRangeLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); return _textRangeLabel; } - (TPCustomButton*)confirmButton { if (!_confirmButton) { _confirmButton = [[TPCustomButton alloc] init]; _confirmButton.title = @"确认"; [_confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside]; } return _confirmButton; } - (void)setOriginStr:(NSString *)originStr { _originStr = originStr; self.snameField.text = originStr; } #pragma mark - button event handler - (void)confirm { [self closeKeyBoard]; if (isBlankString(self.originStr)) { ShowTextMessage(@"请输入昵称"); return; } NSDictionary *dic = @{@"sname":self.originStr}; [Remote doJsonAction:0 requestUrl:editUserInfoURL parameter:dic delegate:self]; } #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); } _originStr = textField.text; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == _snameField) { [self confirm]; } 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 - remote delegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responseData { if (actionTag == 0) { userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; //个人中心修改昵称后,在我的社区中昵称仍为原来的名字(bug:5023) [[NSNotificationCenter defaultCenter]postNotificationName:CHANGEUSERNAMESUCCESS object:nil]; [self.navigationController popViewControllerAnimated:YES]; } } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ ShowTextMessage(message); } #pragma mark - tap handler - (void)tap:(id)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. } @end