I have custom text field in SwiftUI, and even I have placeholder text like
view.text "Put your text here"
in makeUIView function, still it is not shown in text field, I do not know what I missed?
view:
struct TextView : View {
@State var text = ""
var body: some View{
VStack {
CustomTxt(txt: self.$txt)
Button(action: {
txt = ""
}, label: {
Image("clear text field")
})
}
}
textfield:
struct CustomTxt : UIViewRepresentable {
@Binding var txt : String
func makeCoordinator() -> Coordinator {
return ResizableTextField.Coordinator(parent1: self)
}
func makeUIView(context: Context) -> UITextView {
let view = UITextView()
view.delegate = context.coordinator
view.text "Put your text here"
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = txt
DispatchQueue.main.async {
self.height = uiView.contentSize.height
}
}
class Coordinator : NSObject, UITextViewDelegate {
var parent : CustomTxt
init(parent1 : CustomTxt) {
parent = parent1
}
func textViewDidBeginEditing(_ textView: UITextView) {
if self.parent.txt == "" {
textView.text = ""
}
}
func textViewDidChange(_ textView: UITextView) {
DispatchQueue.main.async {
self.parent.height = textView.contentSize.height
self.parent.txt = textView.text
}
}
}
}