Adding color to text within a button

Hello. I added a menu bar link to a site and tried to set the color to blue. I don't get any error with the code when adding .foregroundColor, but the text remains black.

import SwiftUI
    struct ContentView: View {
        @Environment(\.openURL) var openURL
     
        var body: some View {
            
            Button("This should be blue")
            {   openURL(URL(string: "https://www.example.com")!)
                
            }    .font(.system(size: 12, weight: .bold)).foregroundColor(.blue)

        }
    }

Screenshot 2022-12-30 at 12.21.53 PM

Have you tried using the .tint(.blue) modifier instead?

macOS buttons do not typically have colored text labels, so the system button styles may not allow for it.

If the tint modifier doesn’t work you could try using the label: closure-based Button initializer and pass in a Text view with the intended formatting as the label, which should work, or use a custom button style to the same effect (you could also see whether .buttonStyle(.plain) was sufficient as rendered in that context).

Hope that helps!

Thanks codename. Using .tint(.blue) changed the color to blue, but only after it was clicked. I'm going to shift gears and look into making this a menu item rather than a button. Thanks again for your help with this one.

1 Like