如何快速设置默认标签颜色

tHa*_*art 3 ios swift ios-darkmode

我正在尝试将我的 iOS 应用程序更新为暗模式,但在代码中设置暗模式颜色时遇到问题。编辑 UITextView 时,我希望文本颜色在深色模式下为白色,在浅色模式下为黑色(这是默认标签颜色),但据我所知,我不知道如何在代码中编写它,我该怎么做做吗?

extension AddCardsVC: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        if #available(iOS 13.0, *) {
            definitionInput.textColor = UIColor.(need default label color)
        } else {
            definitionInput.textColor = UIColor.black
        }
        if(definitionInput.text == "organizing items into familiar, manageable units; often occurs automatically"){
            definitionInput.text = ""
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 7

或用于UIColor.label获取系统标签颜色。


小智 6

您已经调用了这行代码,它在暗模式和亮模式下都可以正常工作。祝你好运

label.textColor = .none


小智 5

   textView.textColor =  UIColor { tc in
            switch tc.userInterfaceStyle {
            case .dark:
                return UIColor.white
            default:
                return UIColor.black
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是最简单的方法,UIColor 可以通过 traitCollection (TC) 传递一个闭包,traitCollection 有一个名为 userInterfaceStyle 的属性,它告诉用户是否使用暗模式,然后你只需实现 switch 语句来选择你想要返回的颜色