Reputation: 107
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
}
}
Upvotes: 0
Views: 417
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
}
Upvotes: 2