< How to make TextField uppercase or lowercase using textCase() | How to mark content as a placeholder using redacted() > |
Updated for Xcode 14.2
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 project
Or use your own images, like this:
Label("Welcome to the app", image: "star")
Download this as an Xcode project
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 project
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 project
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")
.foregroundColor(.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 project
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.