Skip to main content

How to show text and an icon side by side using Label

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to show text and an icon side by side using Label 관련

SwiftUI by Example

Back to Home

How to show text and an icon side by side using Label | SwiftUI by Example

How to show text and an icon side by side using Label

Updated for Xcode 15

Updated in iOS 15

SwiftUI has a dedicated view type for showing text and icons side by side, which will be particularly helpful for menus, lists, and more.

To use labels, you can either use SF Symbols like this:

Label("Your account", systemImage: "folder.circle")

Download this as an Xcode projectopen in new window

A folder symbol inside a circle beside the text “Your Account”.
A folder symbol inside a circle beside the text “Your Account”.

Or use your own images, like this:

Label("Welcome to the app", image: "star")

Download this as an Xcode projectopen in new window

A yellow star beside the text “Welcome to the app”.
A yellow star beside the text “Welcome to the app”.

You can scale the text and icon in parallel using the font() modifier, like this:

Label("Your account", systemImage: "person.crop.circle")
    .font(.title)

Download this as an Xcode projectopen in new window

A circular person symbol beside the text “Your Account”.

You can control how the label is displayed by applying the labelStyle() modifier using .titleOnly, .iconOnly, and .titleAndIcon, like this:

VStack {
    Label("Text Only", systemImage: "heart")
        .font(.title)
        .labelStyle(.titleOnly)

    Label("Icon Only", systemImage: "star")
        .font(.title)
        .labelStyle(.iconOnly)

    Label("Both", systemImage: "paperplane")
        .font(.title)
        .labelStyle(.titleAndIcon)
}

Download this as an Xcode projectopen in new window

The words “Text Only”. The outline of a star. A paper airplane symbol beside the word “Both”.
The words “Text Only”. The outline of a star. A paper airplane symbol beside the word “Both”.

Important

If you're using Xcode 12, you need to use TitleOnlyLabelStyle(), IconOnlyLabelStyle(), and TitleAndIconLabelStyle() instead. TitleAndIconLabelStyle() is only available from iOS 14.5.

If you want, you can provide entirely custom views for the text and image, like this:

Label {
    Text("Paul Hudson")
        .foregroundStyle(.primary)
        .font(.largeTitle)
        .padding()
        .background(.gray.opacity(0.2))
        .clipShape(Capsule())
} icon: {
    RoundedRectangle(cornerRadius: 10)
        .fill(.blue)
        .frame(width: 64, height: 64)
}

Download this as an Xcode projectopen in new window

A blue rounded rectangle beside a grey capsule containing “Paul Hudson”.
A blue rounded rectangle beside a grey capsule containing “Paul Hudson”.
Similar solutions…
How to hide the label of a Picker, Stepper, Toggle, and more using labelsHidden() | SwiftUI by Example

How to hide the label of a Picker, Stepper, Toggle, and more using labelsHidden()
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to force views to one side inside a stack using Spacer | SwiftUI by Example

How to force views to one side inside a stack using Spacer
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared
How to format text inside text views | SwiftUI by Example

How to format text inside text views

이찬희 (MarkiiimarK)
Never Stop Learning.