记用了12个if做的textField输入框的变态需求

记用了12个if做的textField输入框的变态需求_第1张图片
效果图.gif

需求介绍:

1.只能输入数字和小数点的输入框不改变键盘样式;
2.输入范围 0 - 20;
3.小数点后最多一位且只能是 ".5";
4.输入框输入数字带单位为"码",后面实时转换为米;

代码实现:
shouldChangeCharactersInRange的代理方法操作即可

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //    NSLog(@"%@",string);
// 1. 判断输入为数字或点
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    int i = 0;
    while (i < string.length) {
        NSString * string1 = [string substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string1 rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            return NO;
            break;
        }
        i++;
    }
    
    NSRange ran = [textField.text rangeOfString:@"."];
    if ([textField.text containsString:@"."]) {//2.判断存在小数点
        
        //3.判断小数点的位数
        if (range.location - ran.location <= 1) {
            if ([string isEqualToString:@""]) {
                _ex_buynum_texField.text = [NSString stringWithFormat:@"%zd",(NSInteger)(_ex_buynum_texField.text.floatValue - 0.5)];
                // 判断"码"的位置
                if (_ex_buynum_texField.text.floatValue < 20) {
                    
                    _maConstraint.constant = 46;
                }
                 // 判断"码"的位置
                if (_ex_buynum_texField.text.floatValue < 10) {
                    
                    _maConstraint.constant = 36;
                }
                
                float ma = _ex_buynum_texField.text.floatValue / 1.0936133;
                _changeMeterLabel.text = [NSString stringWithFormat:@"(合%.2f米)",ma];
                return NO;
            }
            return YES;
        }else{
            return NO;
        }
    }

// 这里延迟0.1s的操作留给大家思考~~~~
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _changeMeterLabel.hidden = NO;
        _maLabel.hidden = NO;
        
        float changeMi = _ex_buynum_texField.text.floatValue;
        
        if (changeMi < 10) {
            _maConstraint.constant = 36;
        }
        
        if ((changeMi < 10) & ([string isEqualToString:@"."])) { //小于10 有小数点的情况
            
            _maConstraint.constant = 50;
            _ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",changeMi + 0.5];
            
        }
         // 判断"码"的位置
        if (changeMi >= 10 & changeMi < 20) {
            _maConstraint.constant = 46;
          // 判断输入'.'自动加0.5
            if ([string isEqualToString:@"."]) {
                _ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",changeMi + 0.5];
                _maConstraint.constant = 56;
            }
        }
        
         // 判断范围
        if (changeMi >= 20) {
            _ex_buynum_texField.text = @"20";
            _maConstraint.constant = 46;
        }
        //判断是否隐藏
        if (_ex_buynum_texField.text.floatValue == 0) {
            _changeMeterLabel.hidden = YES;
            _maLabel.hidden = YES;
        }
        
        // 计算米
        float ma = _ex_buynum_texField.text.floatValue / 1.0936133;
        _changeMeterLabel.text = [NSString stringWithFormat:@"(合%.2f米)",ma];
        
    });
    
    return YES;
}

  • 里面有些地方是可以抽取和要写成宏的,这里为方便介绍就不做操作了 有些地方写的繁琐,所以本代码仅供参考
  • 键盘效果配合 'IQKeyboardManager'使用效果更佳
  • 延迟0.1s的操作留给大家思考~~~~


    记用了12个if做的textField输入框的变态需求_第2张图片
    @end.jpeg

你可能感兴趣的:(记用了12个if做的textField输入框的变态需求)