Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

441
Visualizações
Change UILabel's textColor in whole app using Swift

Is there any way in Swift i can change my UILabel's text color property at once in whole app? I've tried using appeareance property but that doesn't work for UILabel textColor. Any way or any library that works on the same.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

One way is using Color Set.

Start with creating new set in your xcassets catalog

enter image description here

... and name it how you want to

enter image description here

Then change your color to color that you need

enter image description here

Finally, set color of your label as this color from xcassets catalog

enter image description here enter image description here

... or programmatically

label.textColor = UIColor(named: "ColorForLabel")

Now when you need to change color of text, just change color of this Color Set

over 4 years ago · Santiago Trujillo Relatório

0

try this in your AppDelegate.swift inside didFinishLaunchingWithOptions function:

UILabel.appearance(whenContainedInInstancesOf: [UIView.self]).textColor = .red //or what color you want
over 4 years ago · Santiago Trujillo Relatório

0

I really liked Arash Etemad's solution, but found it quite extreme as it overrode ALL colours, even if some of them were customised. That's not great when working on an existing large project.

So I came up with this (Swift 5.2):

extension UILabel {

    override open func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)

        guard newSuperview != nil else {
            return
        }

        if #available(iOS 13.0, *) {
            if textColor == UIColor.label {
                textColor = .red
            }
        } else if textColor == UIColor.darkText {
            textColor = .red
        }
    }    
}

It uses the labels own lifecycle event to override default system font colour. With the appearance of dark mode in iOS 13, that can be identified as UIColor.label, whereas before it's UIColor.darkText.

This prevents a custom font colour from being overriden (unless your custom colour is the same as the default ?!? ), whilst not requiring to manually set font colour all over the project.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda