3

i tried to add UITextView to swiftUI because there are things that TextEditor isn't capable of doing. Here's how I built it

struct TextViewSwift : UIViewRepresentable {
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.text = "Testing UITextView"
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        
    }
    
    typealias UIViewType = UITextView
    
}

So I test it out with UILabel (because I want to make sure that the code is working when doing with other UIkit component)

and turns out when I debug the view, not even the UITextView appear, is it a bug within the SwiftUI it self or am I missing something? Thank you

Only UILabel appear

2
  • Does this answer your question stackoverflow.com/a/65181467/12299030? Commented Feb 7, 2022 at 15:19
  • i've read and implement it, still no Commented Feb 7, 2022 at 15:30

2 Answers 2

0

Just need a Text view? Try this.

import SwiftUI

struct TextArea: View {
    @Binding var text: String
    let placeholder: String
    
    init(_ placeholder: String, text: Binding<String>) {
        self._text = text
        self.placeholder = placeholder
        UITextView.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            if text.isEmpty {
                Text(placeholder)
                    .foregroundColor(Color(.placeholderText))
                    .padding(.horizontal, 8)
                    .padding(.vertical, 12)
            }
            
            TextEditor(text: $text)
                .padding(4)
        }.font(.body)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

there are things that TextEditor is not capable at this point, this does not answer my question
0

import SwiftUI

struct CustomTextEditor: UIViewRepresentable {

@Binding var text: String

func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.delegate = context.coordinator
    textView.isScrollEnabled = true
    textView.showsVerticalScrollIndicator = false
    textView.showsHorizontalScrollIndicator = false
    textView.font = ....
    textView.backgroundColor = UIColor.clear
    return textView
}

func updateUIView(_ uiView: UITextView, context: Context) {
    uiView.text = text
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

class Coordinator: NSObject, UITextViewDelegate {
    var parent: CustomTextEditor
    
    init(_ parent: CustomTextEditor) {
        self.parent = parent
    }
    
    func textViewDidChange(_ textView: UITextView) {
        parent.text = textView.text
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.