在网上看到别人发表的文章,我试着做了一下,实现了限制小数点后两位的输入,但是再输入其它文字就不行了,下面是我做的改进
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSRange ran =[textField.textrangeOfString:@"."];
unichar single =[stringcharacterAtIndex:0];//当前输入的字符
constNSInteger limited =2;//限制小数点的位
if (ran.location ==NSNotFound) {
}else {
if(single >=48 && single <=57){
if (range.location - ran.location <=limited) {
returnYES;
}else{
returnNO;
}
}else{
returnYES;
}
}
return YES;
}