This code just format as currency on the fly a number while the user types (Brazilian format)
But I was not able to center the text in it.
What am I doing wrong?
import SwiftUI
struct Testes: View {
@State var myText: String = ""
var body: some View {
HStack{
CurrencyField(text: $myText, placeholder: "Valor")
}
}
}
struct CurrencyField: View {
@Binding var text: String
var placeholder: String
var body: some View {
HStack{
TextField(placeholder, text: $text)
.onChange(of: text, perform: { oldValue in
text = formatCurrency(textoValor: oldValue)
})
.keyboardType(.decimalPad)
.frame(width: 200, height: 40, alignment: .center)
}
.border(Color.black, width: 1)
}
func formatCurrency(textoValor: String) -> String {
let numbers = textoValor.replacingOccurrences(of: "[^0-9]", with: "", options: .regularExpression)
let n2 = Double(numbers) ?? 0
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.decimalSeparator = ","
formatter.groupingSeparator = "."
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.minimumIntegerDigits = 1
formatter.locale = Locale(identifier: "pt_BR")
return formatter.string(from: NSNumber(value: n2 / 100)) ?? "$0"
}
}
Post
Replies
Boosts
Views
Activity
People,
I am a very BEGINNER in swiftui programming, so my excuses for any fool question.
I have a POST (URLSession) that is updating my variables only in the second run (I will explain)
I put a print to see where code is passing and in immediate panel shows like this:
Passed 01 ===========
Passed 03 ===========
Passed 04 =========== out off func!!
2021-05-04 11:41:16.460925-0300 Fuel[2348:100030] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
Passou 02 ===========
Status: true
Nome: Roberto Pires
(I didn't understand libquic failed, but the problem is another one)
Well, the correct should be:
Passed 01 ===========
Passou 02 ===========. - SHOULD PASS HERE FIRST
Status: true
Nome: Roberto Pires
Passed 03 ===========
Passed 04 =========== out off func!!
2021-05-04 11:41:16.460925-0300 Fuel[2348:100030] [] nw_protocol_get_quic_image_blockinvoke dlopen libquic failed
My code is called by a button, but returns BEFORE complete (and as so, show an incorrect Alert, because the "myJsonResponse?.status"_ is false at this time), but the code is still running inside de func (surprisingly) and complete all the POST information correctly.
Why is not entering inside URLSession.shared.dataTask lines in the first time ?
Here is my code:
func validatingLogin() {
guard let myUrl = URL(string: "http://www.rcpires.com/apps/main.asp") else {
print("URL invalid")
return
}
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"
let postString = usuario.loginPost()
request.httpBody = postString.data(using: String.Encoding.utf8)
print(“Passed 01 ===========")
URLSession.shared.dataTask(with: request) { data, response, error in
print("Passed 02 ===========")
guard let myJson = data else {
print(“Data not found")
return
}
if let decodedData = try? JSONDecoder().decode(MyJSONResponse.self, from: myJson) {
DispatchQueue.main.async {
self.myJsonResponse = decodedData
print("Status: \(myJsonResponse?.status ?? false)")
print("Nome: \(myJsonResponse?.nome ?? "")")
return
}
} else {
let dataString = String(decoding: myJson, as: UTF8.self)
print (“Invalid answer: \(dataString.description)")
return
}
}.resume()
print("Passed 03 ===========")
}
This is called by my Login Button:
Button(action: {
validatingLogin()
if aspStatus.success == false {
print("Passed 04 ========== out off func!”)
isAlert = true
} else {
isLogin = true
}
}, label: {
HStack {
Image("checkmark").frame(width: 25, height: 15, alignment: .leading)
Text("Entrar").bold()
}
})
Thanks in advance to every good soul that helps me.