SwiftUI:为按钮的文本添加阴影

SwiftUI: Add shadow to Button's text

我有一个按钮样式,我需要使用这个样式给文本添加阴影。我想做的是:

struct A: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration
            .label
            .shadow()
    }
}

但是由于某种原因没有影子,我做错了吗?

至少,您缺少一个 radius 参数。但是,即使那样,它也会很微妙。如果你给它更多的信息,你可以让它更明显,然后调整它以适应你的需要:

.shadow(color: .pink, radius: 4, x: 5, y: 5)

您错过了阴影颜色和半径

struct FilledButton: ButtonStyle {
   func makeBody(configuration: Configuration) -> some View {
       configuration
         .label
         .shadow(color: .green, radius: 1
                , x: 1.5, y: 1.5)
   }
}

然后,在您的body

中使用它
var body: some View {
    Button("Button 1") {}
        .buttonStyle(FilledButton())
}

这是输出