【问题标题】:How can I change a variable based on different booleans?如何根据不同的布尔值更改变量?
【发布时间】:2021-03-12 02:34:51
【问题描述】:

我可以知道如何缩短这段代码吗?

我正在尝试仅更改Text 的值,并且我已经写了很多行。我可以知道如何更改它并使其简短吗?

if (selectedPage == OnboardingCard.last?.id) {
                                        Text("Get Started!")
                                            .foregroundColor(Color.white)
                                            .fontWeight(.bold)
                                            .padding(10)
                                            .padding(.horizontal)
                                            .background(Color.blue)
                                            .clipShape(Capsule())
                                    } else if (selectedPage == OnboardingCard.first?.id) {
                                        Text("Let's go!")
                                            .foregroundColor(Color.white)
                                            .fontWeight(.bold)
                                            .padding(11)
                                            .padding(.horizontal)
                                            .background(Color.blue)
                                            .clipShape(Capsule())
                                    } else {
                                        Text("Next")
                                        .foregroundColor(Color.white)
                                        .fontWeight(.bold)
                                        .padding(10)
                                        .padding(.horizontal)
                                        .background(Color.blue)
                                        .clipShape(Capsule())
                                    }

【问题讨论】:

  • 你真的需要.padding(11).padding(10) 吗?
  • 只有一个没问题

标签: ios swift if-statement swiftui boolean


【解决方案1】:

首先,定义一个包含所有视图修饰符的扩展,如下所示:

extension Text {
    func bluePaddingModifier(paddingValue: CGFloat) -> some View {
        self
            .foregroundColor(Color.white)
            .fontWeight(.bold)
            .padding(paddingValue)
            .padding(.horizontal)
            .background(Color.blue)
            .clipShape(Capsule())
   }
}

然后,您可以使用如下修饰符。

if (selectedPage == OnboardingCard.last?.id) {
    Text("Get Started!")
        .bluePaddingModifier(paddingValue: 10)
} else if (selectedPage == OnboardingCard.first?.id) {
    Text("Let's Go!")
        .bluePaddingModifier(paddingValue: 11)
} else {
    Text("Next")
        .bluePaddingModifier(paddingValue: 10)
}

如果您不关心填充值是 11 还是 10,那么您可以简单地在 if else 语句的末尾应用视图修饰符,如下所示:

var text = Text("")
if (selectedPage == OnboardingCard.last?.id) {
    text = Text("Get Started!")
} else if (selectedPage == OnboardingCard.first?.id) {
    text = Text("Let's Go!")
} else {
    text = Text("Next")
}
return text.bluePaddingModifier(paddingValue: 10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2021-01-24
    相关资源
    最近更新 更多