Home > Net >  custom coloring with if else in swiftui?
custom coloring with if else in swiftui?

Time:03-12

I am making an earthquake application on SwiftUI. The data I receive includes the magnitude of the earthquake and I want the text I print on the screen to change color according to this magnitude.

For example, I can say: I want it to show green if it is less than 3, yellow if it is between 3 and 5, red if it is between 5 and 7, black if it is greater than 7.

Can you help me?

Thanks

CodePudding user response:

You can use ternary operators to show green if it is less than 3, yellow if it is between 3 and 5, red if it is between 5 and 7, black if it is greater than 7. For example:

struct ContentView: View {
    @State var quake = 2
    
    var body: some View {
        Text("quake in            
  • Related