ios 按下按钮时更改文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24830890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change color of text when button is pushed?
提问by skyguy
I know how to change the background color but what about the actual text? There doesn't seem to be a member on a UIButton called "color" or anything like that. My code:
我知道如何更改背景颜色,但实际文本呢?UIButton 上似乎没有名为“颜色”或类似名称的成员。我的代码:
@IBAction func yellowBtnClicked(sender: UIButton) {
gameboard.image = UIImage(named: "Yellow_gb")
resultsView.image = UIImage(named: "Yellow_results")
colorsView.image = UIImage(named: "Yellow_colors")
colorsBtn.color = UIColor.brownColor() //This line has the issue
}
回答by codester
There is no property setter color
in UIButton
. use instead setTitleColor
.Write this in viewWillAppear
没有的属性设置color
在UIButton
。改用setTitleColor
.Write this inviewWillAppear
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)
This will change title color to brown for UIControlState.Normal
这会将标题颜色更改为棕色 UIControlState.Normal
To set the color of title
when button is in Highlighted
state use
设置title
按钮处于Highlighted
状态时的颜色使用
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)
回答by mobilecat
Swift 3.0 example:
Swift 3.0 示例:
colorsBtn.setTitleColor(UIColor .white, for: UIControlState.normal)
colorsBtn.setTitleColor(UIColor .black, for: UIControlState.highlighted)