1. 自动添加其他字符串(空格)显示
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = [textField text];
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}
text = [text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
// 如果是电话号码格式化,需要添加这三行代码
NSMutableString *temString = [NSMutableString stringWithString:text];
[temString insertString:@" " atIndex:0];
text = temString;
NSString *newString = @"";
while (text.length > 0) {
NSString *subString = [text substringToIndex:MIN(text.length, 1)];
newString = [newString stringByAppendingString:subString];
if (subString.length == 1) {
newString = [newString stringByAppendingString:@" "];
}
text = [text substringFromIndex:MIN(text.length, 1)];
}
newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
// if (newString.length >= 14) {
// return NO;
// }
[textField setText:newString];
return NO;
}
2. 输入金额限制
限制条件:
- 提现金额必须大于 20元
- 只能输入两位小数
- 不满足条件的 提交按钮不能点击
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// 限制只能输入数字
BOOL isHaveDian = YES;
if ([string isEqualToString:@" "]) {
return NO;
}
if ([textField.text rangeOfString:@"."].location == NSNotFound) {
isHaveDian = NO;
}
if ([string length] > 0) {
//判断按钮是否可点击
CGFloat cashMoney = [self.cashMoney doubleValue];
NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([str doubleValue]>=[_showLimitMoney floatValue]&&[str doubleValue]<=cashMoney)
{
//按钮点击事件
commitBtn.backgroundColor = greenColor;
commitBtn.enabled = YES;
}else
{
commitBtn.backgroundColor = cGrayLightColor;
commitBtn.enabled = NO;
return YES;
}
unichar single = [string characterAtIndex:0];//当前输入的字符
if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确
if([textField.text length] == 0){
if(single == '.') {
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}
//输入的字符是否是小数点
if (single == '.') {
if(!isHaveDian)//text中还没有小数点
{
isHaveDian = YES;
return YES;
}else{
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}else{
if (isHaveDian) {//存在小数点
//判断小数点的位数
NSRange ran = [textField.text rangeOfString:@"."];
if (range.location - ran.location <= 2) {
return YES;
}else{
return NO;
}
}else{
return YES;
}
}
}else{//输入的数据格式不正确
[textField.text stringByReplacingCharactersInRange:range withString:@""];
return NO;
}
}
else
{
//判断按钮是否可点击
CGFloat cashMoney = [self.cashMoney doubleValue];
NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([str doubleValue]>=[_showLimitMoney doubleValue]&&[str doubleValue]<=cashMoney)
{
//按钮点击事件
commitBtn.backgroundColor = greenColor;
commitBtn.enabled = YES;
}else
{
commitBtn.backgroundColor = cGrayLightColor;
commitBtn.enabled = NO;
return YES;
}
return YES;
}
}