更改按钮文本和状态的颜色

时间:2022-11-04 21:44:56

I need to change the color of my button's text. I also need to change the state to Disabled after the user presses it.

我需要更改按钮文本的颜色。我还需要在用户按下后将状态更改为Disabled。

I have no idea how to do this. I've been looking things up for a while but they're all either in objective C or I can't understand it (usually help docs, they're stupid.).

我不知道该怎么做。我一直在寻找一段时间,但他们都是客观的C或我无法理解它(通常帮助文档,他们是愚蠢的。)。

4 个解决方案

#1


25  

To change color of text

改变文字的颜色

button.titleLabel.textColor = UIColor.grayColor()

To change state, on button press add following -

要更改状态,请在按钮上按添加以下项 -

button.enabled = true

IBAction method should be like -

IBAction方法应该像 -

@IBAction func buttonTapped(sender : UIButton!) {
    sender.enabled = false
}

#2


61  

In swift you change color for a specific State with the setTitleColor method.

在swift中,您可以使用setTitleColor方法更改特定State的颜色。

In you case it will be :

在你的情况下,它将是:

button.setTitleColor(UIColor.grayColor, forState: UIControlState.Normal)

#3


16  

Swift 3

斯威夫特3

button.setTitleColor(UIColor.gray, for: UIControlState.normal)

Note that;

注意;

  • grayColor has been renamed to gray
  • grayColor已重命名为灰色
  • Normal is now normal (lowercase)
  • 正常现在正常(小写)

You have to set the text colour for the specific button state.

您必须为特定按钮状态设置文本颜色。

#4


3  

For Swift3, try below code :

对于Swift3,请尝试以下代码:

 @IBAction func butnClicked(sender : UIButton) {
     sender.setTitleColor(.red, for: .normal)
     sender.isEnabled = false
 }

Set Enabled and text color from story board.

从故事板设置已启用和文本颜色。

更改按钮文本和状态的颜色

#1


25  

To change color of text

改变文字的颜色

button.titleLabel.textColor = UIColor.grayColor()

To change state, on button press add following -

要更改状态,请在按钮上按添加以下项 -

button.enabled = true

IBAction method should be like -

IBAction方法应该像 -

@IBAction func buttonTapped(sender : UIButton!) {
    sender.enabled = false
}

#2


61  

In swift you change color for a specific State with the setTitleColor method.

在swift中,您可以使用setTitleColor方法更改特定State的颜色。

In you case it will be :

在你的情况下,它将是:

button.setTitleColor(UIColor.grayColor, forState: UIControlState.Normal)

#3


16  

Swift 3

斯威夫特3

button.setTitleColor(UIColor.gray, for: UIControlState.normal)

Note that;

注意;

  • grayColor has been renamed to gray
  • grayColor已重命名为灰色
  • Normal is now normal (lowercase)
  • 正常现在正常(小写)

You have to set the text colour for the specific button state.

您必须为特定按钮状态设置文本颜色。

#4


3  

For Swift3, try below code :

对于Swift3,请尝试以下代码:

 @IBAction func butnClicked(sender : UIButton) {
     sender.setTitleColor(.red, for: .normal)
     sender.isEnabled = false
 }

Set Enabled and text color from story board.

从故事板设置已启用和文本颜色。

更改按钮文本和状态的颜色