|
//
// editSnameController.m
// ThePaperHD
//
// Created by scar1900 on 15/3/26.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "editSnameController.h"
#define TEXTFIELDTAG 2000
@interface editSnameController () {
NSString *sname;
}
@end
@implementation editSnameController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
self.view.frame = CGRectMake(0, 0, loginPopUpSize.width, loginPopUpSize.height);
[self.view addSubview:self.closeBtn];
[self.view addSubview:self.titleLabel];
self.closeBtn.frame = CGRectMake(0, 0, 50, 80);
[self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
self.titleLabel.frame = CGRectMake(0,
0,
CGRectGetWidth(self.view.bounds),
80);
self.keyboardView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.keyboardView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), loginPopUpSize.width, loginPopUpSize.height-80);
[self.keyboardView addSubview:self.editNameField];
self.editNameField.text = [TPUserDefault instance].userBO.sname;
sname = [TPUserDefault instance].userBO.sname;
[self addLine];
[self.keyboardView addSubview:self.editRangeLabel];
self.editRangeLabel.text = @"请输入4-20个字符";
[self.keyboardView addSubview:self.confirmBtn];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
[KGModal sharedInstance].tapOutsideToDismiss = NO;
__weak typeof(self) Self = self;
[[KGModal sharedInstance] setTapHandler:^{
[Self closeKeyBoard];
}];
}
- (UIButton*)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_closeBtn setImage:Image(@"login/popUpBack.png") forState:UIControlStateNormal];
[_closeBtn setImage:Image(@"login/popUpBack_h.png") forState:UIControlStateHighlighted];
[_closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (UILabel*)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.text = @"修改昵称";
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = appFont(30, NO);
}
return _titleLabel;
}
- (UITextField*)editNameField {
if (!_editNameField) {
_editNameField = [[UITextField alloc]initWithFrame:CGRectMake(15,
130/2,
loginPopUpSize.width-15,
35)];
_editNameField.delegate = self;
_editNameField.backgroundColor = [UIColor clearColor];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入昵称" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:LINECOLOR]}];
_editNameField.attributedPlaceholder = atrString;
_editNameField.textColor = [UIColor colorWithHexString:TextBlack];
_editNameField.textAlignment = NSTextAlignmentLeft;
_editNameField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_editNameField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_editNameField.keyboardType = UIKeyboardTypeDefault;
_editNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
_editNameField.tag = TEXTFIELDTAG+0;
// [_editNameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
_editNameField.font = appFont(18, NO);
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(editChange:)
name:UITextFieldTextDidChangeNotification
object:_editNameField];
}
return _editNameField;
}
- (UILabel*)editRangeLabel {
if(!_editRangeLabel) {
_editRangeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.editNameField.frame)-100, CGRectGetMaxY(self.editNameField.frame)+7, 190/2, 25/2)];
_editRangeLabel.textAlignment = NSTextAlignmentRight;
_editRangeLabel.textColor = [UIColor colorWithHexString:LINECOLOR];
_editRangeLabel.backgroundColor = [UIColor clearColor];
}
_editRangeLabel.font = appFont(22/2, NO);
return _editRangeLabel;
}
- (void)addLine {
UIView *userLine = [[UIView alloc]initWithFrame:CGRectMake(15,
CGRectGetMaxY(self.editNameField.frame),
CGRectGetWidth(self.view.bounds)-15,
1)];
userLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
[self.keyboardView addSubview:userLine];
}
- (TPCustomButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [[TPCustomButton alloc]initWithFrame:CGRectMake(190/2,
CGRectGetMaxY(self.editNameField.frame)+200,
loginPopUpSize.width-190,
40)];
_confirmBtn.title = @"确 认";
[_confirmBtn addTarget:self action:@selector(confirmEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
#pragma mark - btn event
- (void)closeEvent:(UIButton*)btn {
[(MLNavigationController*)self.navigationController popViewControllerWithFlip];
}
//- (void)textFieldDidChange:(UITextField *)textField {
// if (textField == self.editNameField) {//【需求】昵称、签名超过字符就不再输入(bug:4087)
// if (textField.text.length >20) {
// textField.text= [textField.text substringToIndex:20];
// }
// }
// sname = textField.text;
//}
-(void) editChange:(NSNotification*) obj{
UITextField *textField = (UITextField *)obj.object;
NSString *toBeString = textField.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > 20) {
textField.text = [toBeString substringToIndex:20];
// sname = textField.text;
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > 10) {
textField.text = [toBeString substringToIndex:10];
// sname = textField.text;
}
}
}
- (void)confirmEvent:(UIButton*)btn {
[self closeKeyBoard];
sname = self.editNameField.text;
if (isBlankString(sname)) {
ShowTextMessage(@"请输入昵称");
return;
}
NSDictionary *dic = @{@"sname":sname};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:editUserInfoURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]);
userBO *oriUser = [TPUserDefault instance].userBO;
user.loginType = oriUser.loginType;
[TPUserDefault instance].userBO = user;
[(MLNavigationController*)Self.navigationController popViewControllerWithFlip];
}else {
ShowTextMessage(message);
}
}];}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
if (isIOS8) {
keyboardHeight = keyboardRect.size.height;
}else {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
keyboardHeight = keyboardRect.size.width;
}else {
keyboardHeight = keyboardRect.size.height;
}
}
self.keyboardOffset = self.responderHeight- (CGRectGetHeight(self.keyboardView.frame)-keyboardHeight)+200;
[self.keyboardView setContentOffset:CGPointMake(0, self.keyboardOffset)];
self.keyboardView.scrollEnabled = YES;
}else {
self.keyboardOffset=0;
[self.keyboardView setContentOffset:CGPointMake(0, self.keyboardOffset)];
}
}
- (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
|