dextermikles
dextermikles

Reputation: 107

How to change color of UITextView by clicking on the button using swift

I have a UITextView, which should have two different colours. At first it should be in default colour, for example black. And when I press button to turn to another mode (incognito) it should change colour of text to the white colour. Here is my code:

   func textViewStartEd(_ textView: UITextView) {
    if textView.textColor == UIColor.lightGray || textView.textColor == UIColor.white {
    textView.text = nil
    textView.textColor = UIColor.black
    }
}

In standard should be black In another mode should be white

Upvotes: 0

Views: 417

Answers (1)

Noor Ahmed Natali
Noor Ahmed Natali

Reputation: 523

-> Create a global variable

private var toggle: Bool = false

-> create a button click event and call a toggle variable logic and ToggleIncongneto Function

@IBAction func btnClick(_ sender: Any) {
    toggle = !toggle // Toggle logic 
    ToggleIncongneto(on: toggle) // toggle func
}

-> Create function to toggle the View

func ToggleIncongneto(on: Bool) {
    txtfld.backgroundColor = on ? .black : .systemGray6 // change background color
    txtfld.textColor = on ? .white : .black // change text color
}

enter image description here

Upvotes: 2

Related Questions