Change Background Color of Texteditor in Swiftui

Change background color of TextEditor in SwiftUI

iOS 16

You should hide the default background to see your desired one:

TextEditor(text: .constant("Placeholder"))
.scrollContentBackground(.hidden) // <- Hide it
.background(Color.red) // To see this


iOS 15 and below

TextEditor is backed by UITextView. So you need to get rid of the UITextView's backgroundColor first and then you can set any View to the background.

struct ContentView: View {
init() {
UITextView.appearance().backgroundColor = .clear
}

var body: some View {
List {
TextEditor(text: .constant("Placeholder"))
.background(Color.red)
}
}
}

Demo

Sample Image

You can find my simple trick for growing TextEditor here in this answer

Background color not change TextEditor view

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

Transparent Background for TextEditor in SwiftUI

Here is a possible work around for this. The extension sets all of your TextViews background to .clear and then you are able to change the background color with .background() modifier.

import SwiftUI

extension NSTextView {
open override var frame: CGRect {
didSet {
backgroundColor = .clear //<<here clear
drawsBackground = true
}

}
}

struct ContentView: View {

@State var string: String = ""

var body: some View {
TextEditor(text: $string)
.textFieldStyle(PlainTextFieldStyle())
.background(Color.red) //<< here red
}
}

Sample Image

Changing TextEditor background color in SwiftUI for macOS

I have just posted an answer for that issue on a similar question here

With the help of extension, you can clear the default background Color of the NSTextView class and then use .background modifier in SwiftUI like this

extension NSTextView {
open override var frame: CGRect {
didSet {
backgroundColor = .clear //<<here clear
drawsBackground = true
}

}
}

struct ContentView: View {

@State var string: String = ""

var body: some View {
TextEditor(text: $string)
.textFieldStyle(PlainTextFieldStyle())
.background(Color.red) //<< here red
}
}

TextEditor added / SwiftUi

First , for changing TextEditor background color
I could not find a SwiftUI 100% solution for that (maybe apple will support this in future ) , so you need to change every UITextView.appearance().backgroundColor in the app

init() {
UITextView.appearance().backgroundColor = UIColor.black
}

don't miss import UIKit

Then for corner radius , you need to use overlay modifier for a rounded corners

VStack {
TextEditor(text: $inputText)
.frame(height:geometry.size.height / 3, alignment: .center)
.lineSpacing(10)
.autocapitalization(.words)
.disableAutocorrection(true)
.padding()

}.overlay(
RoundedRectangle(cornerRadius: 25)
.stroke(Color.yellow, lineWidth: 5)
)
.padding()


Related Topics



Leave a reply



Submit