您的位置:首页 > 产品设计 > UI/UE

UItextField

2015-08-26 16:26 357 查看
UItextField相关代码如下:
- (UITextField *)textField
{
if (_textField == nil) {
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
_textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
_textField.layer.borderWidth = 0.5;
_textField.layer.cornerRadius = 3;
_textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.leftViewMode = UITextFieldViewModeAlways;
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
_textField.font = [UIFont systemFontOfSize:15.0];
_textField.backgroundColor = [UIColor whiteColor];
_textField.placeholder = @"hello word!";
_textField.returnKeyType = UIReturnKeyDone;
_textField.delegate = self;
}

return _textField;
}


- (void)creatUItextField{

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
[textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型

textField.placeholder = @"默认显示的字"; //默认显示的字

textField.secureTextEntry = YES; //密码

textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
textField.delegate = self;

UITextField *iptext = [[UITextField alloc]initWithFrame:CGRectMake(45, 435, 300, 45)];
[iptext setBackgroundColor:[UIColor whiteColor]];
[iptext setKeyboardType:UIKeyboardTypeNumberPad];
[iptext setTextAlignment:NSTextAlignmentCenter];
[iptext setUserInteractionEnabled:NO];
[iptext setPlaceholder:@"输入正确服务器地址"];
[iptext setDelegate:self];
[iptext.layer setBorderColor:[UIColor blackColor].CGColor];
[iptext.layer setBorderWidth:1];
}

//从其他地方退出键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self.textview resignFirstResponder];
}

//当你按下键盘上的retun键时
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

[self.numTF resignFirstResponder];
return YES;
}
//当行输入文本框将要开始编辑时
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

return  YES;
}
//当输入文本开始进入编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField{

}
//将要完成编辑时
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}

//已经退出模式时
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%@",textField.text);
}


//键盘弹出与隐藏
- (void)viewWillAppear:(BOOL)animated{

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboarHidd) name:UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboarShow) name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboarHidd{

[UIView animateWithDuration:0.1 animations:^{
self.loginView.frame = CGRectMake(BOUND_WIDTH/3.5, 100, BOUND_WIDTH/2.3, BOUND_HEIGHT - 120);
}];
}

- (void)keyboarShow{

[UIView animateWithDuration:0.1 animations:^{
CGRect frame= self.loginView.frame;
frame.origin.y -= 160;
self.loginView.frame = frame;
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//监听软键盘 事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

#pragma mark - keyboard
-(void)WillChangeFrame:(NSNotification *)notif{

NSDictionary *info = [notif userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
//收起软键盘的时候
if (value.CGRectValue.origin.y>400) {
[UIView animateWithDuration:.2 animations:^{
self.toolbar.frame = CGRectMake(0, self.view.bounds.size.height-45, 320, 45);
}];
}
else
{
//弹出软键盘的时候
[UIView animateWithDuration:.2 animations:^{
CGRect frame = self.toolbar.frame;
frame.origin.y = self.view.bounds.size.height -  keyboardSize.height - self.toolbar.bounds.size.height;
self.toolbar.frame = frame;
}];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: