Home > Mobile >  SwiftUI Label with dynamic image
SwiftUI Label with dynamic image

Time:04-10

It is possible to use a dynamic image within a Label ?

Something like :

Label("SP98", uiImage: myFunctionToReturnImageFromText("test"))

used in this context :

Menu {
                            
                            if fuelTypeisAvailable(fuelType: "Diesel") {
                                
                                Button {
                                    updateStuff()
                                    Haptics.shared.play(.light)
                                    preferredFuelType = "Diesel"
                                    
                                } label: {
                                    Label("Diesel", systemImage: "fuelpump.circle")
                                   
                  

          }
                        }

Thanks

CodePudding user response:

Try this

struct ImageLabel: View {
    
    let title: String
    let image: Image
    
    public init(_ title: String, uiImage: UIImage) {
        self.title = title
        self.image = Image(uiImage: uiImage)
    }
    
    public init(_ title: String, image: Image) {
        self.title = title
        self.image = image
    }
    
    var body: some View {
        Text(title)
        image
    }
}

CodePudding user response:

I think the initializer that you want to use is:

Label {
   Text("SP98")
} icon: {
   Image(uiImage: myFunctionToReturnImageFromText("test"))

}
  • Related