SwiftUI TextField cursor colour

ID : 274478

viewed : 50

Tags : textfieldswiftuicarettextfield





Top 4 Answer for SwiftUI TextField cursor colour

vote vote

96

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) 
vote vote

89

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

72

This for me worked to change the cursor color for TextEditor. I put the code below as my app initialiser, thus making all of my apps cursors (TextEditor) pink.

init() {     UITextView.appearance().tintColor = .systemPink } 

At the time of writing I have Xcode 13.4.1

vote vote

68

Top 3 video Explaining SwiftUI TextField cursor colour







Related QUESTION?