How to show text and an icon side by side using Label
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")
Or use your own images, like this:
Label("Welcome to the app", image: "star")
You can scale the text and icon in parallel using the font()
modifier, like this:
Label("Your account", systemImage: "person.crop.circle")
.font(.title)
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)
}
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)
}