Changing Texteditor Background Color in Swiftui for MACos

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
}
}

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(.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(.red)
}
}
}

Demo

Sample Image

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

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

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

SwiftUI/macOS: DatePicker without background field

Here is a simplified demo of representable wrapper approach

Tested with Xcode 13.2 / macOS 12.1

Sample Image

struct DemoView: View {
@State private var newDate: Date = Date()

var body: some View {
VStack {
Text("Selected: \(newDate)")
MyDatePicker(selection: $newDate)
}
}
}

struct MyDatePicker: NSViewRepresentable {
@Binding var selection: Date

func makeNSView(context: Context) -> NSDatePicker {
let picker = NSDatePicker()
picker.isBordered = false
picker.datePickerStyle = .textField
picker.action = #selector(Coordinator.onValueChange(_:))
picker.target = context.coordinator
return picker
}

func updateNSView(_ picker: NSDatePicker, context: Context) {
picker.dateValue = selection
}

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

class Coordinator: NSObject {
private let owner: MyDatePicker
init(owner: MyDatePicker) {
self.owner = owner
}

@objc func onValueChange(_ sender: Any?) {
if let picker = sender as? NSDatePicker {
owner.selection = picker.dateValue
}
}
}
}

SwiftUI - How do I change the text color of a Textfield?

.foregroundColor actually does change the text color of TextField but only when it has a default value, for example this will work fine:

TextField(.constant("Hello World"), placeholder: Text("Type here..."))
.foregroundColor(.green)

But once you remove the whole text, text field loses not only its color but the rest of its modifiers such as its alignment as well. This is likely a bug in the current version so I'll file a bug report.


Update: This issue was fixed with Xcode 11 beta 3.

SwiftUI: macOS Can't change TabView's tabItem accent color

On macOS it can be changed this way:

struct ContentView: View {
init() {
UserDefaults.standard.set(1, forKey: "AppleAccentColor") // << here !!
}
var body: some View {
TabView {

Tested with Xcode 13.4 / macOS 12.4

demo

*see for other values https://stackoverflow.com/a/51695756/12299030

**also some related https://stackoverflow.com/a/68846972/12299030

How to set the background color of a swiftui to lightGray?

You can set the background by adding .padding() to the TextField and then using UIColor.lightGray. Setting the .padding() isn't required, however it makes it look a little more native when setting a background color.

TextField("Enter Your Email", text: $text)
.padding()
.background(Color(UIColor.lightGray))


Related Topics



Leave a reply



Submit