I'm trying to create a custom phone formatter. I found PhoneNumberKit that could work for my particular case, the problem with this is that when I tried it there are multiple issues like:
- If using
.international
the program enters in a loop and becomes irresponsive - It requires the number to contain the country's phone-code inside the text to be able to format it correctly, but as I have already done this part in a Button with a flag + phone code on the left, it's not necessary.
So, what I'm trying to do is to create my own PhoneFormatter, something like: (###) ###-####
, currently this might suffice as we're only targeting a single country and this is the most common format used here.
I'm following the code from this site to get an idea of how to do it, but here they are working with dates:
struct ContentView: View {
static let taskDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
var dueDate = Date()
var body: some View {
Text("Task due date: \(dueDate, formatter: Self.taskDateFormat)")
}
}
Someone asked something similar 2 years ago on Apple's Developer Forums but I believe it was for UIKit rather than SwiftUI and there's no code shown there.
I'm trying to attach my formatter directly to my own TextField
, I've tried looking for something similar with SwiftUI and I haven't found anything, and still can't find myself how to create my own custom formatter using something like formatter.numberStyle = "(###) ###-####"
Could anyone point me in the right direction?