I am using this UITextField in SwiftUI because there seems to be no way to deal with firstResponder before iOS 15 where focused was introduced. But the problem I am having has to do with the text inside the UITextField. When I totally fill the field with text, the first letters start showing up on the left side of the field (outside the field). Any idea how to prevent this? There are two commented lines in makeUIView that I also tried including but with no luck.
MyCustomTextField(text: self.$word,
placeholderText: "text",
width: -20-2*0.1*geo.size.height+geo.size.width,
height: 0.1*geo.size.height
)
.frame(width: -20-2*0.1*geo.size.height+geo.size.width, height: 0.1*geo.size.height)
struct MyCustomTextField: UIViewRepresentable {
@Binding var text: String
var placeholderText: String
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
var didBecomeFirstResponder = false
init(text: Binding<String>) {
_text = text
}
func textFieldDidChangeSelection(_ textField: UITextField) {
text = textField.text ?? ""
}
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
//textField.layer.masksToBounds = true
//textField.clipsToBounds = true
textField.delegate = context.coordinator
textField.textColor = .black
textField.placeholder = self.placeholderText
textField.textAlignment = .center
return textField
}
func makeCoordinator() -> Coordinator {
return Coordinator(text: $text)
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<MyCustomTextField>) {
uiView.text = text
uiView.font = UIFont(name: "Arial", size: 20)
}
}