【问题标题】:Limit for 2 decimal after point with currency symbol in UITextFieldUITextField 中带有货币符号的小数点后 2 位的限制
【发布时间】:2018-02-27 07:33:17
【问题描述】:

我需要为 UITextField 设置限制,例如只接受 2 个小数点并添加自动货币符号。

例如,

$23.45 - 对

但是 45.4545 美元 - 错误

$45.4545.65 - 错误

45.45 -Wrong(货币符号应该在那里-如果文本字段有值则自动添加)

我达到了小数点后 2 位的限制,但货币符号卡住了。

注意:

(1) 用户在 UITextField 中输入时必须自动添加货币符号

(2) 如果没有值则显示占位符值。(去掉货币符号)

(3) UITextField 应该接受 1 个点和 2 个小数,例如 $67.89

(4) 所有这些条件都应该在 UITextFieldDelegate 方法中是可能的。不要使用静态标签或其他东西。

这是我的代码...

 //MARK: - UITextFieldDelegate


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

    isEditingMode = true
    switch textField.tag {
    case tag_price_txt:
        let formatter = NumberFormatter()
        formatter.minimumFractionDigits = 2
        formatter.maximumFractionDigits = 2
        formatter.minimumIntegerDigits = 1
        formatter.maximumIntegerDigits = 10

        //var newString = ""

        let aString = textField.text!
        let newStr = aString.replacingOccurrences(of: "£", with: "")
        textField.text = newStr

        let nsString = newStr as NSString?
        let newString:String = (nsString?.replacingCharacters(in: range, with: string))!

        let expression = "^[0-9]*((\\.|,)[0-9]{0,2})?$"
        let regex = try? NSRegularExpression(pattern: expression, options: .caseInsensitive)
        let numberOfMatches: Int? = regex?.numberOfMatches(in: newString, options: [], range: NSRange(location: 0, length: (newString.count)))

      if numberOfMatches != 0 {

                amountTypedString = newString
                let newString1 = "\(SymbolOfCurrency.Pound.rawValue)" +  amountTypedString
                displayAmountString = newString1
                //textField.text = newString1
                print("assigned value",displayAmountString)
                return true
          }else{
            return false
        }

        return true
}

输入后应该是这样的:

请帮忙。提前致谢。

【问题讨论】:

  • 您无法将货币直接动态添加到文本中。反正不容易。您每次都必须正确更新插入符号,而您可以将其显示在文本字段旁边作为不可编辑的标签。添加/删除焦点丢失(模糊)的货币。仅使用shouldChangeCharactersIn 确保输入的数字有效。
  • 没关系,但如果文本字段中没有任何文本,则在用户输入时显示占位符其他添加货币符号,注意:tableView 中的文本字段。
  • 顺便说一句,大多数语言在右侧显示货币符号。英语是例外之一。如果您考虑翻译成其他语言,请确保您可以正确处理他们的规则。
  • 另外,您不必使用NSRegularExpression 来检查字符串是否与模式匹配。 newString.rangeOf(expression, options: .regularExpression) != nil 也会这样做。
  • 不,这不是我的答案,抱歉

标签: ios swift uitextfield uitextfielddelegate


【解决方案1】:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let dotString = "."
    let character = "£"

    if let text = textField.text{
        if !text.contains(character){
            textField.text = "\(character) \(text)"
        }
        let isDeleteKey = string.isEmpty

        if !isDeleteKey {
            if text.contains(dotString) {
                if text.components(separatedBy: dotString)[1].count == 2 || string == "."  {
                    return false
                }
            }
        }
    }
    return true
}

【讨论】:

    【解决方案2】:

    您可以在其中传递您的输入值并将返回字符串设置为输出

    extension String {
            var thousandFormatted: String {
                let numberFormatter = NumberFormatter()
                let number = numberFormatter.number(from: self) ?? 0
                numberFormatter.numberStyle = .currency
        //        numberFormatter.locale = Locale(identifier: "es_CL")
                let string = numberFormatter.string(from: number)
                return string ?? "0.00"
            }
        }
    

    像这样使用 lit:

    var desiredString = inputString.thousandFormatted
    

    注意: 默认情况下,数字格式化程序选择本地货币符号。 如果要指定固定货币,请使用区域设置标识符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2011-12-23
      • 2014-10-03
      • 1970-01-01
      • 2019-06-11
      相关资源
      最近更新 更多