0

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
            }
        }
    }
    
   
}
  • I don't see you adding any placeholder logic to your delegate. If you have to use `UITextView` for some reasons, check out answers under [this question](https://stackoverflow.com/questions/1328638/placeholder-in-uitextview). – Pylyp Dukhov Mar 24 '22 at 10:38

0 Answers0