1

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?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Consider this approach https://stackoverflow.com/a/59509242/12299030 – Asperi Aug 20 '20 at 17:45
  • @Asperi I must admit this approach might be what I want, it blows my mind as I'm not that experienced with SwiftUI. As far as I understand, this is a SwiftUI View that is wrapping an UITextField but it confuses me on how you make it to add those commas. WOW – Frakcool Aug 20 '20 at 18:17
  • @Asperi would you consider explaining a little bit with comments on your linked answer how you make those changes please? Or a little tip on how could I build over it to my specific use case? – Frakcool Aug 20 '20 at 18:53

0 Answers0