Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set textColor of UILabel

I'm currently working on a dynamic form that's originated from UITableViewCells Xibs.

if disabled {
    self.passTitleLabel.textColor = UIColor(named: "Dark Blue")
    self.textField.textColor = UIColor(named: "Dark Blue")
} else {
    self.passTitleLabel.textColor = UIColor(named: "Light Blue")
    self.textField.textColor = .white
}

The UILabel (passTitleLabel) keeps the color set on the Storyboard file and doesn't change as expected. The UILabel is enabled and not highlighted but it is still preserving the color on the storyboard.

All the colors are working in other classes (they're on Assets.xcassets). The Label is properly set with an IBOutlet.

like image 543
ghearly Avatar asked Dec 07 '25 06:12

ghearly


1 Answers

Ran into the same issue, took me a while but finally found an answer. Looks like your UITableViewCell or UICollectionViewCell is running in a background thread, UI related operations need to be done in the main thread or it can lead to these type of problems.

if disabled {
   DispatchQueue.main.async {
      self.passTitleLabel.textColor = UIColor(named: "Dark Blue")
      self.textField.textColor = UIColor(named: "Dark Blue")
   }
} else {
   DispatchQueue.main.async {
      self.passTitleLabel.textColor = UIColor(named: "Light Blue")
      self.textField.textColor = .white
   }
}
like image 78
ealmada Avatar answered Dec 09 '25 20:12

ealmada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!