Currently the cursor/caret for my SwiftUI TextField
is blue. Is there an API for cursor colour or a workaround?
Step 1: Locate the file where you have placed the TextField widget. Step 2: Inside the TextField widget, add the cursorColor parameter and set the color of your choice.
TextField view doesn't provide a way to add background color but we can use the background modifier to add color to the TextField.
SwiftUI's TextField supports placeholder text just like UITextField did – gray text that is shown in the text field when it's empty, either giving users a prompt (“Enter your password”) or showing some example data.
struct ContentView: View { @State private var name: String = "Tim" var body: some View { VStack(alignment: . leading) { TextField("Enter your name", text: $name) Text("Hello, \(name)!") } } } When that's run, you should be able to type into the text field and see a greeting appear directly below.
EDIT: as pointed out below and in the documentation, my original answer (below) is now deprecated and you should use .tint
instead. Or if you want all text fields and other UI elements in your app to be the same you can customize your app’s global accentColor
.
Original answer:
Tried it out and .accentColor
does the trick.
TextField(" Enter some text", text: $name)
.accentColor(.yellow)
The other answer didn't work for me but this did! It doesn't look like the cursor plays well with SwiftUI custom line spacing however...
extension NSTextView {
open override var frame: CGRect {
didSet {
insertionPointColor = .black
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With