我正在尝试在textField上添加一种包含十进制数字的格式。但是,我只得到了这种格式 R $ 150.500.300 ,而不是 R $ 150.500,300 。我在做什么错了?
我希望用户数字也能加上小数,例如R $ 1.500,50或,500
这是我的代码的某些部分:
fileprivate struct Constants {
static let letterMaskCharacterUpper: Character = "A"
static let letterMaskCharacterLower: Character = "a"
static let numberMaskCharacter: Character = "0"
static let wildMaskCharacter: Character = "#"
}
class FormNumericFieldDelegate: NSObject, UITextFieldDelegate {
var controlVM: FormControlViewModel
var maxValue: Double?
var minValue: Double?
var regexKeyPress: String?
var regexLostFocus: String?
var maskType: String?
var maskValue: String?
init(controlVM: FormControlViewModel, maxValue: Double? = nil, minValue: Double? = nil, regexKeyPress: String? = nil, regexLostFocus: String? = nil, maskType: String? = nil, maskValue: String? = nil) {
self.controlVM = controlVM
self.maxValue = maxValue
self.minValue = minValue
self.regexKeyPress = regexKeyPress
self.regexLostFocus = regexLostFocus
self.maskType = maskType
self.maskValue = maskValue
super.init()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let aSet = NSCharacterSet(charactersIn:"0123456789,.").inverted
let compSepByCharInSet = string.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet.joined(separator: "")
if string != numberFiltered {
showErrorMessage(textField: textField)
return false
}
let currentText: NSString = textField.text as NSString? ?? ""
var rangeNew = range
rangeNew.location = range.location - 1
var newText = ""
if let mType = maskType, mType == "Percentage" {
if rangeNew.location >= 0 {
newText = (currentText.replacingOccurrences(of: "%", with: "") as NSString).replacingCharacters(in: rangeNew, with: string)
} else {
newText = currentText.replacingCharacters(in: range, with: string)
}
} else {
newText = currentText.replacingCharacters(in: range, with: string)
}
//Regex validation
if let regexKeyPress = self.regexKeyPress,
let textbox = textField as? NumericTextField {
if newText.range(of: regexKeyPress, options: .regularExpression) == nil {
showErrorMessage(textField: textField)
return false
}else{
textbox.errorMessage = ""
}
}
//MaxValue validation
if let maxValue = self.maxValue, let newValue = Double(newText.replacingOccurrences(of: "%", with: "")) {
if newValue > maxValue {
showErrorMessage(textField: textField)
return false
}
}
//Mask transformation
if let mType = maskType, var mValue = maskValue {
if mType == "Decimal" || mType == "Currency" || mType == "Percentage" {
if mValue.isEmpty {
mValue = "0"
}
do {
if let convertedValue = try Conversion.convertStringNumber(obj: newText.digits, tClass: NSNumber.self) {
let dividedValue = convertedValue.decimalValue / pow(10, 0)
controlVM.setValue(path: .value, value: "\(dividedValue)")
return false
}
}
catch {
print(error.localizedDescription)
}
}
}
controlVM.setValue(path: .value, value: newText)
if let textBox = textField as? FormNumericTextField {
if textBox.controlViewModel != nil {
do {
try textBox.controlViewModel?.setValueFromView(value: newText)
} catch {
print(error.localizedDescription)
}
}
}
return false
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if let textbox = textField as? NumericTextField {
textbox.errorMessage = ""
}
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
//Regex validation
if let regexLostFocus = self.regexLostFocus,
let text = textField.text,
let textbox = textField as? NumericTextField {
if text.range(of: regexLostFocus, options: .regularExpression) == nil, let placeholder = textbox.placeholder {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(resetErrorMessage(textbox:)), object: textbox)
textbox.errorMessage = "\(placeholder) - \(NSLocalizedString("regex_error_pattern_dont_match", comment: ""))"
}else{
textbox.errorMessage = ""
}
}
//MinValue validation
if let minValue = self.minValue, let newText = textField.text, let newValue = Double(newText.replacingOccurrences(of: "%", with: "").replacingOccurrences(of: ",", with: "")) {
if newValue < minValue {
showErrorMessage(textField: textField)
}
}
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
} else {
textField.resignFirstResponder()
}
return true
}
private func showErrorMessage(textField: UITextField) {
if let textbox = textField as? NumericTextField {
if let placeholder = textbox.placeholder {
textbox.errorMessage = "\(placeholder) - \(NSLocalizedString("regex_error_pattern_dont_match", comment: ""))"
}else{
textbox.errorMessage = NSLocalizedString("regex_error_pattern_dont_match", comment: "")
}
}
}
@objc private func resetErrorMessage(textbox: NumericTextField) {
textbox.errorMessage = ""
}
我确实有向我返回货币格式的功能:
private class func getCurrencyFormat(format: String) throws -> NumberFormatter {
if let formatter = dateFormatterCache.object(forKey: "c" + format as NSString) as? NumberFormatter {
return formatter
} else {
let newFormatter = NumberFormatter()
newFormatter.usesGroupingSeparator = true
newFormatter.numberStyle = .currency
newFormatter.locale = Locale.current
newFormatter.minimumFractionDigits = Int(format) ?? 0
newFormatter.maximumFractionDigits = Int(format) ?? 0
dateFormatterCache.setObject(newFormatter, forKey: "c" + format as NSString)
return newFormatter
}
}
问题图片: