Sajjon
Sajjon

Reputation: 9917

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

Upvotes: 6

Views: 5025

Answers (2)

Asperi
Asperi

Reputation: 258541

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())
    }
}

Upvotes: 13

user2731717
user2731717

Reputation: 75

@Sajjon You can add a custom View as a workaround and use Image with Text inside a HStack

Upvotes: -2

Related Questions