美文网首页
UITextField 实现价格输入框

UITextField 实现价格输入框

作者: codermali | 来源:发表于2017-09-11 12:48 被阅读12次

只能输入八位数字,只能输入一个小数点,值保留两位小数

首先设置UITextField

    _moneyTextField = [[UITextField alloc] init];
    _moneyTextField.textColor = FONTCOLOR_BLACK;
    _moneyTextField.clearButtonMode = UITextFieldViewModeAlways;
    _moneyTextField.keyboardType = UIKeyboardTypeNumberPad;
    _moneyTextField.delegate = self;
    
    UILabel *logoLabel = [[UILabel alloc] init];
    logoLabel.text = @"¥";
    logoLabel.textColor = FONTCOLOR_BLACK;
    logoLabel.textAlignment = NSTextAlignmentCenter;
    logoLabel.font = [UIFont systemFontOfSize:20];
    logoLabel.frame = CGRectMake(0, 0, 20, 20);
     _moneyTextField.leftView = logoLabel;
    _moneyTextField.leftViewMode = UITextFieldViewModeAlways;
    [self addSubview:_moneyTextField];
    [_moneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(moneyLabel.mas_bottom).offset(20);
        make.left.equalTo(self.mas_left).offset(20);
        make.right.equalTo(self.mas_right).offset(-20);
        make.height.mas_equalTo(44);
    }];

然后就是利用UITextField的代理方法

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

/**
 *  textField.text  已经输入的文本
 *  range           被替换的字符的range,输入时为{x, 0},删除时为{x, 1}
 *  string          输入的文本(删除时为空)
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *filterStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
    
    if ([string isEqualToString:filterStr])
    {
        if (textField.text.length == 0)
        {
            //如果输入第一位是.
            if ([string isEqualToString:@"."])
            {
                [SVProgressHUD showInfoWithStatus:@"第一位不能是小数点"];
                return NO;
            }
            //如果输入第一位是0
            if ([string isEqualToString:@"0"])
            {
                [SVProgressHUD showInfoWithStatus:@"第一位不能是0"];
                return NO;
            }
        }
        
        //如果输入小数
        if ([textField.text containsString:@"."])
        {
            //只能输入一个小数点
            if ([string isEqualToString:@"."])
            {
                [SVProgressHUD showInfoWithStatus:@"只能输入一个小数点哦"];
                return NO;
            }
            //小数点后只能输入两位小数
            NSRange dotRange = [textField.text rangeOfString:@"."];
            if ((range.location - dotRange.location) > 2)
            {
                [SVProgressHUD showInfoWithStatus:@"只能输入两位小数哦"];
                return NO;
            }
        }
        
        if (textField.text.length - range.length + string.length > 8) return NO;
        return YES;
    }
    else
    {
        return NO;
    }   
}

相关文章

网友评论

      本文标题:UITextField 实现价格输入框

      本文链接:https://www.haomeiwen.com/subject/bbzxsxtx.html