Hey, writing again due to difficulties in completing this example.
Despite the bad user experience, I want the text to be formatted dynamically as I type, rather than at the end and make space after every 2 letters.
Everything works fine until we want to edit a string within our TextField. I want to perform these operations on a limited text, a maximum of 13 characters, so I think it's possible to do but still have some issues with text editing.
Has anyone done something similar or has an idea on how to fix this?
struct CustomTextFieldView: UIViewRepresentable {
@Binding var text: String
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.delegate = context.coordinator
textField.text = text
textField.placeholder = "Enter text here"
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(text: $text)
}
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
init(text: Binding<String>) {
self._text = text
}
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
// Get the current text in the textField
guard let currentText = textField.text,
let textRange = Range(range, in: currentText) else {
return true
}
// Construct the new text after replacement
let newText = currentText.replacingCharacters(in: textRange, with: string)
// Disallow input if the total character count exceeds 13 or contains a space
if !string.isEmpty {
if newText.count > 13 || string.contains(" "){
return false
}
// Insert a space after every 2 characters
if newText.count % 3 == 0 {
let insertionIndex = newText.index(newText.startIndex, offsetBy: newText.count - 1)
textField.text?.insert(" ", at: insertionIndex)
}
}
return true
}
}
}
struct ContentView: View {
@State var text = ""
var body: some View {
VStack {
CustomTextFieldView(text: $text)
}
}
}