1.今天学习了textfiled的使用方法,在界面的设计中textfiled 使用的十分广泛:
2.我们主要学习一下textfiled的代理方法:
3.我们要设置textfiled 的代理方法和实现代理<uitextfiledDeegate>
4.改变textfiled的背景颜色,实现方法如下:代理方法如下
-(void)textFieldDidBeginEditing:(UITextField *)textField;
#pragma mark -UITextFieldDelegate
//开始编辑的时候
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.tag ==0 ){
textField.background = [UIImage imageNamed:@"enter.png"];
}else if (textField.tag ==1){
textField.background = [UIImage imageNamed:@"enter.png"];
}else if (textField.tag ==2){
textField.background = [UIImage imageNamed:@"enter.png"];
}else if(textField.tag ==3){
textField.background = [UIImage imageNamed:@"enter.png"];
}
}
//结束编辑的时候
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.tag ==0 ){
textField.background = [UIImage imageNamed:@"enter2.png"];
}else if (textField.tag ==1){
textField.background = [UIImage imageNamed:@"enter2.png"];
}else if (textField.tag ==2){
textField.background = [UIImage imageNamed:@"enter2.png"];
}else if (textField.tag ==3){
textField.background = [UIImage imageNamed:@"enter2.png"];
}
}
5.textfiled代理的方法:限制输入文字的长度:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
#pragma mark - UITextFiled
// 判断限制输入框的长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (self.addressTF == textField) //判断是否时我们想要限定的那个输入框
{
if ([toBeString length] > 50) { //如果输入框内容大于20则弹出警告
textField.text = [toBeString substringToIndex:50];
[MBProgressHUD showError:@"超过最大字数不能输入了"];
return NO;
} }
else if (self.nameTF == textField){
if ([toBeString length]>20) {//如果输入框内容大于10弹出警告
textField.text = [toBeString substringToIndex:20];
[MBProgressHUD showError:@"超过最大字数不能输入了"];
return NO;
}
}
return YES;
}