Yonus
Yonus

Reputation: 233

How can I change a variable based on different booleans?

Can I know how to shorten this code?

I am trying to change the value of my Text only and I have written so many lines. Can I know how to change it and make it short?

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())
                                    }

Upvotes: 1

Views: 63

Answers (1)

rohanphadte
rohanphadte

Reputation: 1018

First, define an extension containing all your view modifiers like this:

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

Then, you can use the modifiers like the following.

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)
}

If you don't care about having a padding value of 11 vs 10, then you can simply apply the view modifiers at the end of the if else statement, like this:

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)

Upvotes: 2

Related Questions