Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI TextField cursor colour

Currently the cursor/caret for my SwiftUI TextField is blue. Is there an API for cursor colour or a workaround?

like image 219
SnoopDogg Avatar asked Oct 20 '19 00:10

SnoopDogg


People also ask

How do I change the cursor color in TextField?

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.

How do I change TextField background color in SwiftUI?

TextField view doesn't provide a way to add background color but we can use the background modifier to add color to the TextField.

What is prompt in TextField SwiftUI?

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.

How do I get TextField value in SwiftUI?

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.


2 Answers

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)
like image 82
shim Avatar answered Oct 07 '22 18:10

shim


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
        }
    }
}
like image 3
Jake Avatar answered Oct 07 '22 19:10

Jake