跳转至
Structure

LabelStyleConfiguration

The properties of a label.

Declaration

struct LabelStyleConfiguration

Overview

Use this structure to access the properties of a Label when creating your own LabelStyles. A LabelStyleConfiguration has two properties:

  1. LabelStyleConfiguration/title: The Label's Text part.
  2. LabelStyleConfiguration/icon: The Label's Image part.

A parameter of this type called configuration is passed to the one required function of the LabelStyle protocol: makeBody(configuration:).

In the example below, we use the configuration parameter in the makeBody(configuration:) function to create our own custom LabelStyle

struct MyLabelStyle: LabelStyle {
    let color: Color

    func makeBody(configuration: Configuration) -> some View {
        VStack {
            configuration.title
            configuration.icon
                .foregroundColor(color)
        }
    }
}
We can then reuse that custom label style:

struct ContentView: View {
    var body: some View {
        VStack {
            Label("Banana", systemImage: "suit.heart.fill")
                .labelStyle(MyLabelStyle(color: .yellow))
            Label("Apple", systemImage: "suit.heart.fill")
                .labelStyle(MyLabelStyle(color: .red))
        }
        .font(.title)
    }
}
A view containing a VStack with two labels, the first with the title "Banana" and a yellow filled heart icon and the second with the title "Apple" and a red filled heart icon.

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Structure

Icon A type-erased icon view of a label.

Title A type-erased title view of a label.


Instance Property

icon A symbolic representation of the labeled item.

title A description of the labeled item.