【问题标题】:SwiftUI: Add shadow to Button's textSwiftUI:为按钮的文本添加阴影
【发布时间】:2021-02-03 05:32:05
【问题描述】:

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

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

但是由于某种原因阴影不存在,是我做错了吗?

【问题讨论】:

  • 谢谢你们,我做错了是我使用了 20 的半径,所以我肯定能看到它,但它使阴影变得如此透明,我根本看不到它) P.S.我省略了 shadow() 参数以不妨碍案例的简单性,但我看到这是我的失败,对不起

标签: button text swiftui shadow


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你错过了阴影颜色和半径

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

    然后,在你的身体里使用它

    var body: some View {
        Button("Button 1") {}
            .buttonStyle(FilledButton())
    }
    

    这是输出

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多