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
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
}
}
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
SwiftUI/macOS: DatePicker without background field
Here is a simplified demo of representable wrapper approach
Tested with Xcode 13.2 / macOS 12.1
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
*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
Displaying State of an Async API Call in Swiftui
Hovering a Modelentity in Front of Arcamera
Swift Delegate Beetween Two Vc Without Segue
Swift Firestore Search for Users
Skscene Becomes Unresponsive While Being Idle
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files
Swift - Encode and Decode a Dictionary [String:Any] into Plist
Swift 4: Validating Credit Card Expiration Date
How to Make Uicollectionview Reload Once It Receives Data from Firebase
Firebase Access Keys in Queryorderby
Find Out When Uikeyboard.Frame Intersects with Other Frame
Swift MAC App, Run Terminal Command Without Knowing the Path (So It Looks in Every Path in $Path)
Swift Callkit Sometimes Can't Activate Loudspeaker After Received Call (Only Incoming Call)
Data Ranged Subscribe Strange Behavior
Problem with Gesture in Xcode 12 and iOS 14
Overlapping Accesses to 'Urlcomponents', But Modification Requires Exclusive Access
Swift 3: Int Is Not Convertible to Bool in Bitwise Operation