Set background color of a SwiftUI label

To change the background color of a SwiftUI label, you can use the background() modifier along with the desired color. Here’s an example of how you can do it:


import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .background( // Set the background color to blue
            .foregroundColor(.white) // Set the text color to white

In the example above, the .background( modifier sets the background color of the label to blue. You can replace with any other color of your choice, such as,, or you can even define your custom color using the Color initializer.

Additionally, you can use the .foregroundColor() modifier to change the text color of the label. In the example above, it’s set to .white to ensure the text is visible against the blue background.

Remember to adjust the code according to your specific SwiftUI view hierarchy and needs.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *