I am using SwiftUI to build an app for iOS, iPadOS and macOS and I need a TextEditor with a transparent background.
This question has been asked before and I found a good answer for iOS/iPadOS (Change background color of TextEditor in SwiftUI), but none for macOS.
This is the code I have based on the link above that works perfectly for iOS and iPadOS:
TextEditor(text: $text)
.foregroundColor(.black)
.onAppear {
#if os(macOS)
// ??
#else
UITextView.appearance().backgroundColor = .clear
#endif
}
NSTextView
doesn't appear to have an equivalent to UITextView.appearance()
, so I'm not entirely sure what to do there.
Using .background(Color.clear)
also doesn't work:
TextEditor(text: $text)
.foregroundColor(.black)
.background(Color.clear)
.onAppear {
#if os(macOS)
// ??
#else
UITextView.appearance().backgroundColor = .clear
#endif
}
Does anyone know of a way to get a transparent background for a TextEditor on macOS?
Also, this question has already been asked specifically about macOS here: Changing TextEditor background color in SwiftUI for macOS, but no answer that works given.