Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Label text and image vertically misaligned

I'm using SwiftUI's brand new Label View, running Xcode 12 beta on Big Sur.

As image I use SF Symbol and found an image named "play". But I've noticed the same problem with custom images without any bordering pixels (i.e. spacing is not caused by the image), e.g. PDF icons, so it is probably not related to the image.

In demos by Apple the Text and the image should just automatically align properly, but I do not see that.

struct ContentView: View {
    var body: some View {
        Label("Play", systemImage: "play")
    }
}

Results in this:

enter image description here

Any ideas why the image (icon) and the text is vertically misaligned?

If we give the Button a background color we see more precisely the misalignment:

Label("Play", systemImage: "play")
    .background(Color.red)

Results in this:

enter image description here

like image 552
Sajjon Avatar asked Dec 31 '22 00:12

Sajjon


1 Answers

Probably a bug, so worth submitting feedback to Apple. Meanwhile here is working solution based on custom label style.

Tested with Xcode 12b

enter image description here

struct CenteredLabelStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        HStack {
            configuration.icon
            configuration.title
        }
    }
}

struct TestLabelMisalignment: View {
    var body: some View {
        Label("Play", systemImage: "play")
           .labelStyle(CenteredLabelStyle())
    }
}
like image 199
Asperi Avatar answered Jan 09 '23 21:01

Asperi