Search code examples
swifttextcolorios-darkmode

Swift change text color to dark/lightmode


I am trying to change the text color in a UITextView from light grey to white/black.

So the new text color should be white when the dark mode is on and the text color should be black when the light mode is on.


Solution

  • You can set the UIColor to .label

    textView.textColor = .label
    

    This should use the system label colour which is back against a white background and vice versa

    You can also create your own dark/light colours in the assets file. enter image description here

    And then use them in code or the interface builder.

    textView.textColor = UIColor.init(named: "myBlueColor")