Steven-Carrot
Steven-Carrot

Reputation: 3101

Background color not change TextEditor view

//.........some other views........

       ZStack(alignment: .leading ) {
            
            Color.black.ignoresSafeArea()
            TextEditor(text: $mytext)
                .background(Color.orange)
                .padding()
                .foregroundColor(Color.gray)
                .frame(width: 362, height: 400)
            

        }

Background color remains Color.white for all cases i applied so what's wrong here? Thank you.

Upvotes: 1

Views: 2461

Answers (3)

Abdul Karim Khan
Abdul Karim Khan

Reputation: 4945

For iOS 16.0 and above, set scrollContentBackground to hidden.

TextEditor(text: $mytext)
    .scrollContentBackground(.hidden)

Upvotes: 1

Asperi
Asperi

Reputation: 258365

We need to clear default background color via appearance

    init() {
        UITextView.appearance().backgroundColor = .clear
    }

and then background modifier works in any mode

    TextEditor(text: $mytext)
        .background(Color.orange)

Tested with Xcode 13.4 / iOS 15.5

demo

Upvotes: 4

Steven-Carrot
Steven-Carrot

Reputation: 3101

You can't really change the background of SwiftUI TextEditor. Either material/shape/color is not possible for applying as a background of SwiftUI TextEditor.

However, you can multiply the color of your TextEditor which provides a similar result to the general background(.color) except it's not really a background color.

 TextEditor(text: $mytext)
            .colorMultiply(.orange) //modified
            .padding()
            .foregroundColor(Color.gray)
            .frame(width: 362, height: 400)

Upvotes: 1

Related Questions