Answers for "how to create custom label in swiftui"

0

label swiftui

//A label is a struct in swiftui
//Declaration:

struct Label<Title, Icon> : View where Title : View, Icon : View

//One of the most common and recognizable user interface components is the combination of an icon and a label.
//This idiom appears across many kinds of apps and shows up in collections, lists, menus of action items, and disclosable lists, just to name a few.
//You create a label, in its simplest form, by providing a title and the name of an image, such as an icon from the SF Symbols collection:

//From Label- SwiftOnTap

//Some common examples include:

Label("Lightning", systemImage: "bolt.fill")

//Makes a HStack with two elements, a text with the name "Lightning", and a sf symbol named bolt.fill
//To customise a label, you can either use the default label customisations by apple, such as:

.labelStyle(IconOnlyLabelStyle())

//Or make your own style


struct RedBorderedLabelStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        Label(configuration)
            .padding()
            .border(Color.red)
    }
}

struct RedBorderedBoltView: View {
    var body: some View {
        Label("Lightning", systemImage: "bolt.fill")
            .labelStyle(RedBorderedLabelStyle())
    }
}

//In this case we make a struct that defines a new label style, which will allow us to use the .labelStyle modifier on it
//The .padding() and .border(Color.red) make it so that the label has a padding and also a border
//(I'll save most of the explaining. This is just to introduce labels in general)
//(For more info please go to the source link)
Posted by: Guest on August-20-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language