繁体   English   中英

SwiftUI 将标签的垂直对齐方式更改为居中图像和文本

[英]SwiftUI change vertical alignment of Label to center image and text

我试图将 SwiftUI 标签的图像和文本垂直居中。 我试图覆盖图像的alignmentGuide,但没有用。 有任何想法吗?

        Label {
            Text("Test fdasf \n adfsa dsfsd f asdf \n asd fasd fads sad fda")
        } icon: {
            Image(systemName: true ? "checkmark.circle.fill" : "circle")
                .alignmentGuide(VerticalAlignment.top) {
                    $0[VerticalAlignment.center]
                }
        }

它们在 y 轴上对齐

我们可以使用自定义标签样式来做到这一点,比如

struct DemoStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        HStack(alignment: .center) {    // << here !!
            configuration.icon
            configuration.title
        }
    }
}

并使用它

Label {
    Text("Test fdasf \n adfsa dsfsd f asdf \n asd fasd fads sad fda")
} icon: {
    Image(systemName: true ? "checkmark.circle.fill" : "circle")
}
.labelStyle(DemoStyle())

演示

使用 Xcode 13 / iOS 15 测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM