ios TextField限制输入两位小数

只需要实现textField的这个代理方法就可以实现

- (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;
    
    
}

楼主亲测好使!

posted @ 2015-03-12 02:27  6度XZ  阅读(692)  评论(0编辑  收藏  举报