SwiftUI Label
Published Dec 2, 2022
A label is a standard user interface component that includes an icon and a title. It appears in many different contexts like collections, lists, or buttons to name a few.
Syntax
Label(myLabelName, systemImage: myImage)
- A
Labelcan be a stand-alone component. myLabelNameis a string that will be displayed on the label as text.myImageis a string that will be displayed on the label as an image.ViewModifierscan be used to display just the icon, text, or both, or automatically adjust based on the space available.
Example
The following example displays a Text view that represents a number and two Buttons. The first Button uses a Label and the second one is a broken-down version of what Label actually accomplishes:
@State private var score = 0var body: some View {VStack(spacing: 20) {Text("\(score)")Button {score += 1} label: {Label("Add", systemImage: "plus")}Button {score -= 1} label: {HStack {Image(systemName: "minus")Text("Remove")}}}}
Each time the user clicks either the top or bottom button, the score above will increase or reduce, respectively, by 1.
This will display the following:

Learn SwiftUI on Codecademy
- Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
- Includes 7 Courses
- With Certificate
- Beginner Friendly.13 hours