如何在 alert.addTextfield 中放置一个 $ 硬币掩码?
how to put a $ coin mask in alert.addTextfield?
如何在alert.addTextfield中放置$硬币掩码?
货币 ?
跟textfield有什么关系?
https://code.i-harness.com/en/q/1c673c6
func showalert(with marcas: Marcas?){
let title = marcas == nil ? "Adicionar" : "Editar"
let alert = UIAlertController(title: title + " Marca", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in textField.placeholder = "Nome da Marca"
if let name = marcas?.nome {
textField.text = name
}
}
alert.addTextField { (textFieldValor) in textFieldValor.placeholder = "Preço"
if let valor = marcas?.valor {
textFieldValor.text = valor
}
}
textFieldValor.addTarget(self, action: #selector(self.myTextFieldDidChange), for: .editingChanged)
@objc func myTextFieldDidChange(_ textField: UITextField) {
if let amountString = textField.text?.currencyInputFormatting() {
textField.text = amountString
}
}
extension String {
// formatting text for currency textField
func currencyInputFormatting() -> String {
var number: NSNumber!
let formatter = NumberFormatter()
formatter.numberStyle = .currencyAccounting
formatter.currencySymbol = "R$"
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
var amountWithPrefix = self
// remove from String: "$", ".", ","
let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count), withTemplate: "")
let double = (amountWithPrefix as NSString).doubleValue
number = NSNumber(value: (double / 100))
// if first number is 0 or all numbers were deleted
guard number != 0 as NSNumber else {
return ""
}
return formatter.string(from: number)!
}
}
如何在alert.addTextfield中放置$硬币掩码? 货币 ? 跟textfield有什么关系? https://code.i-harness.com/en/q/1c673c6
func showalert(with marcas: Marcas?){
let title = marcas == nil ? "Adicionar" : "Editar"
let alert = UIAlertController(title: title + " Marca", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in textField.placeholder = "Nome da Marca"
if let name = marcas?.nome {
textField.text = name
}
}
alert.addTextField { (textFieldValor) in textFieldValor.placeholder = "Preço"
if let valor = marcas?.valor {
textFieldValor.text = valor
}
}
textFieldValor.addTarget(self, action: #selector(self.myTextFieldDidChange), for: .editingChanged)
@objc func myTextFieldDidChange(_ textField: UITextField) {
if let amountString = textField.text?.currencyInputFormatting() {
textField.text = amountString
}
}
extension String {
// formatting text for currency textField
func currencyInputFormatting() -> String {
var number: NSNumber!
let formatter = NumberFormatter()
formatter.numberStyle = .currencyAccounting
formatter.currencySymbol = "R$"
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
var amountWithPrefix = self
// remove from String: "$", ".", ","
let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count), withTemplate: "")
let double = (amountWithPrefix as NSString).doubleValue
number = NSNumber(value: (double / 100))
// if first number is 0 or all numbers were deleted
guard number != 0 as NSNumber else {
return ""
}
return formatter.string(from: number)!
}
}