iOS中UILabel的文本颜色在深色模式下没有更新。

4

我有一个在应用程序中显示时间段的集合视图。在暗模式下,UILabel似乎无法在白色背景上显示黑色文本颜色。

在故事板中,我已将标签的颜色设置为黑色(也尝试了默认颜色)。

在代码中,当用户选择单元格时,

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

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

而我有UILabel的扩展:

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
            }

        }
    }

}

结果如下:

enter image description here enter image description here


https://noahgilmore.com/blog/dark-mode-uicolor-compatibility/ - koen
4个回答

8

确保 UILabel 的颜色在 .dark 模式下显示有两种解决方案:

  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
    }
    

或者你可以简单地设置self.textColor = UIColor(named: "MyBlackColor")

希望这能帮到你。

输入图像描述


2

在集合视图中,标签的显示效果与预期不符。我尝试了不同的配置方式,但都没有解决。最后我使用了按钮,这种方式可以实现效果。一旦我能够让标签正常工作,我会更新我的答案。


0

默认颜色在iOS 13中不会像黑色一样工作,因为默认颜色已经改为UIlabel的颜色而不是黑色。

尝试在设置背景颜色后设置文本颜色。


我尝试了默认颜色和另一种黑色。两者都不起作用。 - Teja Nandamuri
是的,默认设置不行。你能否尝试使用以下代码:self.backgroundColor = UIColor.black self.textColor = UIColor.white而不是self.textColor = UIColor.white self.backgroundColor = UIColor.black - Reed
你需要在iOS 13中使用UIColor.label等,请参见我上面发布的链接。 - koen

0

如果您使用默认标签颜色,则在深色模式下标签颜色为白色,在浅色模式下为黑色。

如果您使用自定义颜色,则标签的颜色为您的自定义颜色。


不,对我来说不起作用。在我的情况下,我需要在两种情况下使用自定义颜色。 - undefined
如果你有任何解决方案,请帮助我。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接