8

I am trying to update my iOS app for dark mode, but am having trouble with setting the dark mode colors in code. Upon editing a UITextView, the color I want the text color to be white in dark mode, and black in light mode (which is default label color), but to my knowledge I do not know how to write this in code, how do I do it?

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 = ""
        }
    }

}
Top-Master
  • 7,611
  • 5
  • 39
  • 71
tHatpart
  • 1,302
  • 3
  • 12
  • 27

5 Answers5

21

Or use UIColor.label to get system label color.

G.Song
  • 310
  • 2
  • 5
10

you have call this line of code , it works fine both dark mode and light mode. goodluck

label.textColor = .none

Raj.Rathod
  • 99
  • 4
  • This works great for UILabels as the documentation says : The default value for this property is the system’s label color, which adapts dynamically to Dark Mode changes. However it does NOT work for me for a UITextView, documentation : The default text color is black. – Michael Pirotte Nov 18 '20 at 11:07
3
   textView.textColor =  UIColor { tc in
            switch tc.userInterfaceStyle {
            case .dark:
                return UIColor.white
            default:
                return UIColor.black
            }
        }

This is the easiest way, the UIColor can be passed a closure with traitCollection (TC) and the traitCollection has a property called userInterfaceStyle which tells if the user is using the dark mode, then you just implement switch statement to choose what color you wanna return

C NASIR
  • 180
  • 3
0

you just need to assign UIColor.systemBackground to your UITextView Text Color

textView.textColor =  UIColor.systemBackground 

it will automatically change text color white in dark mode and text color black in light mode

Chetan Hedamba
  • 293
  • 2
  • 9
  • You have it opposite. This would make the text white in light mode, and dark in dark mode.... – Eric May 28 '21 at 21:17
0
textView.textColor =  .placeholderText
shim
  • 9,289
  • 12
  • 69
  • 108