Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How to show Text depends on switch case in SwiftUI?

I am working with SwiftUI, I have one switch case function, depend on that switch case i want to show diff color and text.

    func status(status: Status){
            switch status {
            case .accepted:
//text "accepted"
                //green text   
    
            case .standby:
//text "standby"
                //yellow text
    
            case .notAllowed:
//text "notAllowed"
                 //red text
            }
        }

VStack(alignment: .leading) {
            Text("Test")
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can simply switch over status inside the body of your view and assign the correct String and foregroundColor to your Text inside each `case.

struct StatusView: View {
    let status: Status

    var body: some View {
        switch status {
        case .accepted:
            Text("accepted")
                .foregroundColor(.green)
        case .standby:
            Text("standby")
                .foregroundColor(.yellow)
        case .notAllowed:
            Text("not allowed")
                .foregroundColor(.red)
        }
    }
}

Or if you can modify Status, you can simply assign a String rawValue to it, then displaying the appropriate text based on its value is even easier.

enum Status: String {
    case accepted
    case standby
    case notAllowed
}

struct StatusView: View {
    let status: Status

    var body: some View {
        Text(status.rawValue)
            .foregroundColor(statusColor(status: status))
    }

    private func statusColor(status: Status) -> Color {
        switch status {
        case .accepted:
            return .green
        case .standby:
            return .yellow
        case .notAllowed:
            return .red
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

Here is an updated and refactored answer based on David answer, with this way you do not need that ststusColor function anymore and you can access the colorValue every where in your project instead of last answer that was accessible only inside StatusView.

struct StatusView: View {
    
    let status: Status
    
    var body: some View {
        
        Text(status.rawValue)
            .foregroundColor(status.colorValue)
        
    }
    
}


enum Status: String {
    
    case accepted
    case standby
    case notAllowed
    
    var colorValue: Color {
        switch self {
        case .accepted:
            return .green
        case .standby:
            return .yellow
        case .notAllowed:
            return .red
        }
    }
    
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!