UITextField 在输入数字的最后添加 3 个零阻止输入
UITextField add 3 zero at last of input number block the typing
我创建了一个 UITextField
允许用户输入唯一的数字,然后在数字字符串的最后自动添加 3 个零(使用点分隔符但现在它只显示为逗号而不是点) .例如,如果我输入 3
,文本字段中的字符串将是 3.000
并限制输入 12 个字符。
但现在我遇到了一个问题,我一次只能输入一个数字,当我输入另一个数字时,它会替换我输入的旧数字。例如,我在文本字段中输入 3
它变成了 3.000
,然后输入 2
它变成了 2.000
而不是 32.000
。下面是我处理输入的代码:
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.textField?.text = textField.text
let strTextOld = textField.text ?? ""
let numStringOld = strTextOld.replacingOccurrences(of: ".", with: "", options: String.CompareOptions.literal, range: nil)
var newValue: String = ""
if let digit = Int(string) {
let doubleItem = Int(numStringOld) ?? 0
let itemNew = ((doubleItem/1000) * 10) + (Int(string) ?? 0)
newValue = "\(itemNew * 1000)"
}
if string == "" {
let doubleItem = Int(numStringOld) ?? 0
let itemNew = Int((doubleItem / 1000) / 10)
newValue = "\(itemNew * 1000)"
}
guard let text: String = textField.text else {return true}
if newValue.length <= 12 {
return self.formatPrice(textFormat: newValue, labelFormat: textField)
}
return true
}
//format price for textfield
func formatPrice(textFormat: String, labelFormat: UITextField) -> Bool {
let formatStyle = NumberFormatter()
formatStyle.numberStyle = .decimal
let number = Double(textFormat.replaceMatches(regexp: "[^0-9]", with: ""))
if let number:NSNumber = NSNumber(value: number ?? 0) {
var strValue:String = formatStyle.string(from: number) ?? ""
formatStyle.decimalSeparator = "."
_ = strValue.replacingOccurrences(of: ",", with: ".")
if number == 0 {
strValue = ""
}
if labelFormat === self.textField {
self.textField?.text = "\(strValue)"
}
return true
}
return true
}
我添加了文档以便您了解每个步骤:
let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.currencySymbol = ""
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
return formatter
}()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let suffix = ".000"
// using NSString will allow us to replase replacing characters in range and get the newString
guard let nsString = textField.text?.replacingOccurrences(of: suffix, with: "") as NSString? else { return false }
// The newString that will be achieved after this function returns
var newString = nsString.replacingCharacters(in: NSRange(location: (range.location - suffix.count) < 0 ? 0 : range.location - suffix.count, length: range.length > nsString.length ? nsString.length : range.length), with: string)
// Going to delete a character.
if string.isEmpty {
// The reason we have an extra "0" is because after we return "true" the last digit will be deleted.
textField.text = !newString.isEmpty ? "\(addCommas(to: newString.replacingOccurrences(of: ",", with: "")))\(suffix)0" : " "
return true
} else if newString.count <= 15 { // Going to add a character.
newString = "\(addCommas(to: newString.replacingOccurrences(of: ",", with: "")))\(suffix)"
textField.text = newString
}
return false
}
func addCommas(to string: String) -> String {
guard let newNumber = Float(string) else { return string }
return formatter.string(from: NSNumber(value: newNumber)) ?? string
}
我创建了一个 UITextField
允许用户输入唯一的数字,然后在数字字符串的最后自动添加 3 个零(使用点分隔符但现在它只显示为逗号而不是点) .例如,如果我输入 3
,文本字段中的字符串将是 3.000
并限制输入 12 个字符。
但现在我遇到了一个问题,我一次只能输入一个数字,当我输入另一个数字时,它会替换我输入的旧数字。例如,我在文本字段中输入 3
它变成了 3.000
,然后输入 2
它变成了 2.000
而不是 32.000
。下面是我处理输入的代码:
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.textField?.text = textField.text
let strTextOld = textField.text ?? ""
let numStringOld = strTextOld.replacingOccurrences(of: ".", with: "", options: String.CompareOptions.literal, range: nil)
var newValue: String = ""
if let digit = Int(string) {
let doubleItem = Int(numStringOld) ?? 0
let itemNew = ((doubleItem/1000) * 10) + (Int(string) ?? 0)
newValue = "\(itemNew * 1000)"
}
if string == "" {
let doubleItem = Int(numStringOld) ?? 0
let itemNew = Int((doubleItem / 1000) / 10)
newValue = "\(itemNew * 1000)"
}
guard let text: String = textField.text else {return true}
if newValue.length <= 12 {
return self.formatPrice(textFormat: newValue, labelFormat: textField)
}
return true
}
//format price for textfield
func formatPrice(textFormat: String, labelFormat: UITextField) -> Bool {
let formatStyle = NumberFormatter()
formatStyle.numberStyle = .decimal
let number = Double(textFormat.replaceMatches(regexp: "[^0-9]", with: ""))
if let number:NSNumber = NSNumber(value: number ?? 0) {
var strValue:String = formatStyle.string(from: number) ?? ""
formatStyle.decimalSeparator = "."
_ = strValue.replacingOccurrences(of: ",", with: ".")
if number == 0 {
strValue = ""
}
if labelFormat === self.textField {
self.textField?.text = "\(strValue)"
}
return true
}
return true
}
我添加了文档以便您了解每个步骤:
let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.currencySymbol = ""
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
return formatter
}()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let suffix = ".000"
// using NSString will allow us to replase replacing characters in range and get the newString
guard let nsString = textField.text?.replacingOccurrences(of: suffix, with: "") as NSString? else { return false }
// The newString that will be achieved after this function returns
var newString = nsString.replacingCharacters(in: NSRange(location: (range.location - suffix.count) < 0 ? 0 : range.location - suffix.count, length: range.length > nsString.length ? nsString.length : range.length), with: string)
// Going to delete a character.
if string.isEmpty {
// The reason we have an extra "0" is because after we return "true" the last digit will be deleted.
textField.text = !newString.isEmpty ? "\(addCommas(to: newString.replacingOccurrences(of: ",", with: "")))\(suffix)0" : " "
return true
} else if newString.count <= 15 { // Going to add a character.
newString = "\(addCommas(to: newString.replacingOccurrences(of: ",", with: "")))\(suffix)"
textField.text = newString
}
return false
}
func addCommas(to string: String) -> String {
guard let newNumber = Float(string) else { return string }
return formatter.string(from: NSNumber(value: newNumber)) ?? string
}