0

I need to be able to style TextField in my SwiftUI macOS application. Specifically, I would need to be able to change the cursor (caret) and the selection color. As I read online, for this people use either older and marked for deprecation .accentColor() function or newer .tint() function. However, it seems like the application simply ignores those functions.

struct ContentView: View { @State private var text = "" var body: some View { VStack { TextField("Text field", text: $text) .tint(.red) } .padding() } } 

This code produces this window. As you can see .tint(.red) is being ignored and orange system Accent Color is being used.

I've tried using older and marked for deprecation .accentColor() - it didn't work. Tried making custom style for TextField and setting .tint()/.accentColor() there - same result.

I'm on macOS 14.6.1 (23G93) and using Xcode version 16.0 (16A242d)

Did I somehow set up the project incorrectly? Or there's something that I am missing here?


EDIT (17.09.24 20:25): I found this answer that suggested subclassing NSTextField. I'd personally like to avoid doing that as it'd require me to reimplement some properties of TextField and it breaks .style(), which I use to apply custom look to the TextField. Also, currently, if I'd want to pass a theme to the init function of that custom NSTextField, it'd ask me to implement other constructors (including init(frame:) without which the app just crashes, even though Xcode isn't saying anything about it).

0

1 Answer 1

1

By default, macOS ignores the tint colour set by applications, as the system-level tint color takes precedence.

In system settings, in the Appearance tab, there is an option called Accent color.

Image

If the accent color is set to multicolor the tint color set by apps is used, for example, where you set the tint color of a text field.

When it is set to another color, like blue (I think this by default, please excuse me if I'm wrong), this will overwrite your setting of the tint colour.

So the solution is simply to change your system preferences to multicolor.


I did a mini-investigation -- I have 2 text fields, one where I set the accent color to red, and the other where I set the tint to red. Strangely enough when I use the tint color it overrides the system accent color but when I use accent color the system accent color takes precedence.

Tint works

*Accent color does not *

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.