zoukankan      html  css  js  c++  java
  • UITextField 限制用户输入小数点后位数的方法

    UITextField 限制用户输入小数点后位数的方法

    位数限制: limited

    在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 = 1;
        for (int i = futureString.length-1; i>=0; i--) {
    
            if ([futureString characterAtIndex:i] == '.') {
                
                if (flag > limited) {
                    return NO;
                }
                
                break;
            }
            flag++;
        }
        
        return YES;
    }
    
  • 相关阅读:
    Codeforces 220C
    Codeforces 697D
    HDU 4417
    Codeforces 396C
    Codeforces 246C
    HDU 6333
    HDU 3389
    总结:树上启发式合并
    HDU 6319
    Codeforces 1009G
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/6868725.html
Copyright © 2011-2022 走看看