zoukankan      html  css  js  c++  java
  • iPad开发--UItextfield中输入类似于数学计算上的数字的方法

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
            NSCharacterSet*cs;
            
            cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
            
            NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            BOOL  basicTest = [string isEqualToString:filtered];
            // 只能输入数字小数点
            if(!basicTest) { // 不相等
                return NO;
            }
            
            if ([textField.text rangeOfString:@"."].location==NSNotFound) {
                isHaveDian=NO;
            }
            
            // 第一位必须为数字不能为小数点
            if ([string length]>0)
            {
                unichar single=[string characterAtIndex:0];//当前输入的字符
                if ((single >='0' && single<='9') || single=='.')//数据格式正确
                {
                    //首字母不能为0和小数点
                    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:@"."];
                                int tt=range.location-ran.location;
    //                            if (tt <= 2){
    //                                return YES;
    //                            }else{
    //                                return NO;
    //                            }
                            }
                            else
                            {
                                return YES;
                            }
                        }
                    }else{//输入的数据格式不正确
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    return YES;
                }
            }
    }

     
  • 相关阅读:
    解决NLPIR汉语分词系统init failed问题
    牛客小白月赛3---G 旅游(树形dp)
    蓝桥杯 能量项链 (区间dp)
    OpenJ_Bailian
    LeetCode#169 Majority Element
    LeetCode#171 Excel Sheet Column Number
    LeetCode#172 Factorial Trailing Zeroes
    this指针
    auto、register、extern以及static
    const与static
  • 原文地址:https://www.cnblogs.com/huangh/p/4186996.html
Copyright © 2011-2022 走看看