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
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
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
}
}
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
Is There an Alternative to Initialize() in MACos Now That Swift Has Deprecated It
Swift/Uiview/Drawrect - How to Get Drawrect to Update When Required
Drag and Reorder - Uicollectionview with Sections
iOS 10 Imessage App Extension: How to Calculate the Height of the Extra Tall Navbar
Avaudioengine Crashes When Plug Headphones in or Out
How to Include Assets/Resources in a Swift Package Manager Library
Present Uiviewcontroller as a Modal with Transparent Background
Rxswift Map and Flatmap Difference
Checking If a Double Value Is an Integer - Swift
Swift Uialertcontroller with Pickerview Button Action Stay Up
How to Copy a Struct and Modify One of Its Properties at the Same Time
Protocol Extension on an Objc Protocol
Swift Combine: How to Create a Single Publisher from a List of Publishers
Expected Hexadecimal Code in Braces After Unicode Escape