[英]How to change color of UITextView by clicking on the button using swift
I have a UITextView, which should have two different colours.我有一个 UITextView,它应该有两种不同的颜色。 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
}
}
-> Create a global variable -> 创建一个全局变量
private var toggle: Bool = false
-> create a button click event and call a toggle variable logic and ToggleIncongneto Function -> 创建按钮单击事件并调用切换变量逻辑和 ToggleIncongneto Function
@IBAction func btnClick(_ sender: Any) {
toggle = !toggle // Toggle logic
ToggleIncongneto(on: toggle) // toggle func
}
-> Create function to toggle the View -> 创建 function 来切换视图
func ToggleIncongneto(on: Bool) {
txtfld.backgroundColor = on ? .black : .systemGray6 // change background color
txtfld.textColor = on ? .white : .black // change text color
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.