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

210
Views
¿Cómo mostrar el texto depende del caso del interruptor en SwiftUI?

Estoy trabajando con SwiftUI, tengo una función de caso de cambio, depende de ese caso de cambio que quiero mostrar color y texto diferentes.

 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

Simplemente puede cambiar el status dentro del cuerpo de su vista y asignar la String y el color de foregroundColor correctos a su Text dentro de cada caso.

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

O si puede modificar Status , simplemente puede asignarle un String rawValue , luego mostrar el texto apropiado en función de su valor es aún más fácil.

 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

Aquí hay una respuesta actualizada y refactorizada basada en la respuesta de David , de esta manera ya no necesita esa función ststusColor y puede acceder a colorValue en todas partes de su proyecto en lugar de la última respuesta a la que solo se podía acceder dentro 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!