如何根据不同的布尔值更改变量?
How can I change a variable based on different booleans?
我能知道如何缩短这段代码吗?
我只是想改变我的 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())
}
首先,定义一个包含所有视图修饰符的扩展,如下所示:
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)
我能知道如何缩短这段代码吗?
我只是想改变我的 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())
}
首先,定义一个包含所有视图修饰符的扩展,如下所示:
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)