Menu
Coddy logo textTech

Backgrounds

Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 15 of 50.

.background draws a color behind a view. It covers the view's area, so padding makes the colored area bigger than the text:

Text("New")
    .padding()
    .background(.red)
    .foregroundStyle(.white)

To round the corners of the colored area, clip it to a shape with .clipShape. RoundedRectangle(cornerRadius: 8) gives rounded corners, Capsule() fully round ends:

Text("Tag")
    .padding(8)
    .background(.blue)
    .clipShape(RoundedRectangle(cornerRadius: 8))
challenge icon

Challenge

Easy

Turn the text into a badge: add the standard padding, a red background and white letters.

Try it yourself

import SwiftUI

@main
struct CoddyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction to SwiftUI

Practice on your own: Swift playground