UITextField限制小数输入的位数

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];

    [futureString  insertString:string atIndex:range.location];

    NSInteger flag=0;

    const NSInteger limited = 2;//小数点后需要限制的个数

    for (int i = futureString.length-1; i>=0; i--) {

        if ([futureString characterAtIndex:i] == '.') {

            if (flag > limited) {

                return NO;

            }

            break;

        }

        flag++;

    }

    return YES;

}

---------------------

作者:Frank_春儿

来源:CSDN

原文:https://blog.csdn.net/qq_29284809/article/details/50732004

版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(UITextField限制小数输入的位数)