-(UITextField*)createField:(NSString*)placeholder andTag:(int)tag andFont:(double)font{

    UITextField * field =[[UITextField alloc]init];
field.frame = CGRectMake(, , Main_Width-, );
field.delegate = self;
field.userInteractionEnabled = YES;
field.textAlignment = NSTextAlignmentLeft;
field.keyboardType = UIKeyboardTypeDefault;
// field.returnKeyType = UIReturnKeySend;
field.tag = tag;
field.placeholder =placeholder;
// field.text = placeholder;
field.textColor = littleBlackColor;
[field setFont:[UIFont fontWithName:@"STHeitiSC" size:font]];
[field addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
return field;
}
-(void)textFieldWithText:(UITextField *)textField{

    UITextField * field = (UITextField*)textField;
switch (textField.tag) {
case ://发车单位/收车单位
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"unitname"]; //送达单位、收车单位
}
break;
case : //联系人
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"contact"];
}
break;
case ://联系电话
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"phone"];
}
break;
case ://详细地址
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"address"];
}
break;
default:
break;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
//string就是此时输入的那个字符textField就是此时正在输入的那个输入框返回YES就是可以改变输入框的值NO相反
if ([string isEqualToString:@"\n"]) //按会车可以改变
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
switch (textField.tag) {
case ://发车单位/收车单位
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"单位输入不能超过20个字符"];
return NO;
}
break;
case : //联系人
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"联系人输入不能超过10个字符"];
return NO;
}
break;
case ://联系电话
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"联系电话输入不能超过11个字符"];
return NO;
}
break;
case ://详细地址
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"详细地址输入不能超过30个字符"];
return NO;
}
break;
default:
break;
} return YES;
} uitextfield输入字符限制-LMLPHPuitextfield输入字符限制-LMLPHP
04-29 02:37