skip to Main Content
struct TextView: View {
    @State private var textInput2: String = ""
    var body: some View {
        ScrollView{
            VStack(spacing: 24){
                TextField("", text: $textInput2)
                    .font(.body)
                    .foregroundColor(.gray)
                    .padding()
                    .frame(height: 142)
                    .background(Color("3"))
                    .cornerRadius(20)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.gray.opacity(0.5), lineWidth: 2)
                    )
                
                TextEditor(text: $textInput2)
                    .textFieldStyle(PlainTextFieldStyle())
                    .font(.body)
                    .foregroundColor(.gray)
                    .padding()
                    .background(Color("3"))
                    .frame(height: 142)
                    .cornerRadius(20)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.gray.opacity(0.5), lineWidth: 2)
                    )
            }
            .padding(.horizontal)
        }
    }
}

I want to change the background color of a TextEditor, I tried:

  • .foregroundColor changed the text color instead of the background color;
  • .background only changed the surroundings’ background color and not the whole TextEditor as seen in the image below:

enter image description here

2

Answers


  1. Add this line to TextEditor:

    .scrollContentBackground(.hidden) // <- Hide it
    
    Login or Signup to reply.
  2. UITextView is the base class of TextEditor. So you need to change UITextView‘s background color to clear when initializing the struct, then you can set any color as the background.

    struct ContentView: View {
        init() {
            UITextView.appearance().backgroundColor = .clear
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search