4

I have a collection view which shows time slots in the app. In the dark mode, it seems the UILabel is not showing the black text color on white background.

In the storyboard, I have set the color as Black (also tried the Default color) for the label.

In the code, when user select the cell,

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

   if let cell = collectionView.cellForItem(at: indexPath) as? TimeCell{
      cell.timeLabel.toggleTheme(true)
   }
 }

and I have the UILabel extension:

extension UILabel{

    func toggleTheme(_ selected : Bool){
        if selected{
            if #available(iOS 13.0, *) {
                if self.traitCollection.userInterfaceStyle == .dark{
                    self.textColor = UIColor.black
                    self.backgroundColor = UIColor.white
                }else{
                    self.textColor = UIColor.white
                    self.backgroundColor = UIColor.black
                }
            } else {
                 self.textColor = UIColor.white
                 self.backgroundColor = UIColor.black
            }

        }else{
            if #available(iOS 13.0, *) {
                if self.traitCollection.userInterfaceStyle == .dark{
                    self.textColor = UIColor.white
                    self.backgroundColor = UIColor.black
                }else{
                    self.textColor = UIColor.black
                    self.backgroundColor = UIColor.white
                }
            } else {
                 self.textColor = UIColor.black
                 self.backgroundColor = UIColor.white
            }

        }
    }

}

and the result is:

enter image description here enter image description here

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

4 Answers4

8

There are 2 solutions to ensure the UILabel color is displayed in .dark mode:

  1. Set the Table color to UIColor.labelColor. This will adopt automatically based on device theme dark or light

  2. The other option is to define color in xcassets and provide color variants for different themes. You need to select Appearance as Any, Dark to get option to provide multiple colours. Refer below image. With defined you do NOT need to check for theme as you are doing in below snippet:

    if #available(iOS 13.0, *) {
        if self.traitCollection.userInterfaceStyle == .dark {
            self.textColor = UIColor.black
            self.backgroundColor = UIColor.white
        } else {
            self.textColor = UIColor.white
            self.backgroundColor = UIColor.black
        }
    } else {
        self.textColor = UIColor.white
        self.backgroundColor = UIColor.black
    }
    

Alternatively you can simply set self.textColor = UIColor(named: "MyBlackColor")

Hope this will help.

enter image description here

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Pankaj Kulkarni
  • 553
  • 4
  • 12
2

Somehow the label in collection view is not working as expected. I tried different configurations and none worked. I ended up using button instead and it worked in my case. I will update my answer once I get this working with a label.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

Default color won't work as a black from iOS 13 because default color has been changed to color of UIlabel instead of black.

try setting up text color after setting background color.

Reed
  • 944
  • 7
  • 15
  • I tried both default color and the other black color. Both didn't work. – Teja Nandamuri Oct 18 '19 at 19:49
  • yes default won't work. can you please try self.backgroundColor = UIColor.black self.textColor = UIColor.white instead of self.textColor = UIColor.white self.backgroundColor = UIColor.black – Reed Oct 18 '19 at 19:49
  • You need to use `UIColor.label`, etc in iOS 13, see the link I posted above. – koen Oct 18 '19 at 19:58
0

If you use default label color, the label color is white for dark mode and black for light mode.

If you use your custom color, tha labor color is your custom color.

Alessandro Pirovano
  • 2,509
  • 28
  • 21