Label in SwiftUI is a view that allows us to have an icon and a text in the same component. Before SwiftUI 2.0, an HStack was usually created to put an icon and then the text, but with this component we have it all in one in our iOS apps.
We use Label in SwiftUI to display an icon and a text in our App. This view was introduced in SwiftUI 2.0 and was presented at WWDC 2020. Its use is very simple, and it allows us to add an icon and a text in the same component.
The SwiftUI framework offers us a view called Label that allows us to add an image and a text together. Instead of using HStack, as we have used in the previous article on how to create a button in SwiftUI with text and images, we can use Label to create the same design.
With the following code in Swift:
import SwiftUI
struct ContentView: View {
var body: some View {
Button {
print("Delete button tapped")
} label: {
Label(
title: {
Text("Macbook")
.fontWeight(.semibold)
.font(.title)
},
icon: {
Image(systemName: "macbook")
.font(.title)
}
)
.padding()
.foregroundStyle(.white)
.background(.blue)
.cornerRadius(40)
}
}
}
We create the same example as in the previous article with an image and text.