【问题标题】:Create custom UITextField with limited number and fraction digits Swift创建具有有限数字和小数位数的自定义 UITextField Swift
【发布时间】:2019-08-10 02:56:16
【问题描述】:

我正在尝试制作一个只能接受数字的自定义 UITextField,但逗号后的数字数量有限。 所以如果用户输入:

0 -> 0
10 -> 10
10,0 -> 10,0
1000 -> 1.000
0,1 -> 0,1
0,00000008 -> 0,00000008
1,000006 -> 1,000006
10000,12345678 -> 10.000,12345678

用户必须输入逗号,以便计算逗号后面的数字,但如果他不输入,它总是一个整数。逗号后的最大字符数为 8。

已经尝试了一些 textField 委托方法,但没有任何成功:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let characterSet = NSCharacterSet(charactersIn: "0123456789,").inverted
    let filtered = string.components(separatedBy: characterSet)
    let component = filtered.joined(separator: "")
    let isNumeric = string == component

    if isNumeric {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.maximumFractionDigits = 8
        formatter.groupingSeparator = "."
        formatter.decimalSeparator = ","

        if let groupingSeparator = formatter.groupingSeparator {

            if string == groupingSeparator {
                return true
            }

            if let textWithoutGroupingSeparator = textField.text?.replacingOccurrences(of: groupingSeparator, with: "") {
                var totalTextWithoutGroupingSeparators = textWithoutGroupingSeparator + string
                if string.isEmpty {
                    totalTextWithoutGroupingSeparators.removeLast()
                }
                if let numberWithoutGroupingSeparator = formatter.number(from: totalTextWithoutGroupingSeparators),
                    let formattedText = formatter.string(from: numberWithoutGroupingSeparator) {

                    textField.text = formattedText
                    return false
                }
            }
        }
    }

    return true
}

【问题讨论】:

  • 很高兴给我们反馈

标签: ios swift uitextfield decimal numberformatter


【解决方案1】:

我已经使用了下面的代码。检查您是否觉得这很有用。

// MARK:- TEXTFIELD DELEGATE
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool
{
    let countdots = (txf_Amount.text?.components(separatedBy: ".").count)! - 1

    if countdots > 0 && string == "."
    {
        return false
    }

    let MAX_BEFORE_DECIMAL_DIGITS = 10
    let MAX_AFTER_DECIMAL_DIGITS = 3
    let computationString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    // Take number of digits present after the decimal point.
    let arrayOfSubStrings = computationString.components(separatedBy: ".")

    if arrayOfSubStrings.count == 1 && computationString.count > MAX_BEFORE_DECIMAL_DIGITS {
        return false
    } else if arrayOfSubStrings.count == 2 {
        let stringPostDecimal = arrayOfSubStrings[1]
        return stringPostDecimal.count <= MAX_AFTER_DECIMAL_DIGITS
    }

    return true

}

【讨论】:

  • 效果很好,但是分组分隔符为'.'或“,”不起作用
猜你喜欢
  • 1970-01-01
  • 2022-06-28
  • 2020-04-26
  • 1970-01-01
  • 2014-10-03
  • 2010-10-05
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多