Swift - 在点击UIButton时更改文本的颜色和大小

时间:2022-11-21 21:30:01

Basically, I have a three buttons on my main screen.

基本上,我的主屏幕上有三个按钮。

When I select one of these buttons, I would like the text on the selected button to change to bold and change color (blue).

当我选择其中一个按钮时,我希望所选按钮上的文本更改为粗体并更改颜色(蓝色)。

When I select a different button, I would like the newly selected button to change to bold and change color(blue), and the previously selected button to go back to normal. (non-bold and black text)

当我选择一个不同的按钮时,我希望新选择的按钮更改为粗体并更改颜色(蓝色),以及之前选择的按钮将恢复正常。 (非粗体和黑色文字)

I have these buttons sending an action to the script.

我有这些按钮向脚本发送动作。

This is what I have, I can't seem to get it to work. Help would be much appreciated!

这就是我所拥有的,我似乎无法让它发挥作用。非常感谢帮助!

@IBAction func buttonOne(sender: UIButton){
    sender.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
}

I have tried .Highlighted and .Selected on the UIControlState, neither seem to work. I have also tried the following, but I cant get it to work.

我试过.Highlighted和。在UIControlState上选择,似乎都不起作用。我也试过以下,但我不能让它工作。

@IBAction func buttonOne(sender: UIButton){
    sender.titleLabel?.textColor = UIColor.blueColor()
}

I figured that since the sender was a UIButton, and it was the button that was clicked, taking the values off of it and resetting them would work. I do believe I am missing something.

我认为既然发件人是UIButton,那就是点击的按钮,取下它的值并重置它们就行了。我相信我错过了一些东西。

Thank you

3 个解决方案

#1


4  

Sounds like you want UIControlState.Normal

听起来像你想要UIControlState.Normal

Selected does nothing in most cases, and Highlighted is only while you're pressing the button. See more info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State

在大多数情况下,“选定”不执行任何操作,只有在按下按钮时才会突出显示。在此处查看更多信息:https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State

#2


2  

Maybe you can do with a transform...

也许你可以做一个变换......

@IBAction func buttonPressed(sender: UIButton) {
    sender.titleLabel!.textColor = UIColor.blueColor()
    sender.transform = CGAffineTransformMakeScale(0.8, 0.8)
}

@IBAction func buttonReleased(sender: UIButton) {
    sender.titleLabel!.textColor = UIColor.redColor()
    sender.transform = CGAffineTransformIdentity
}

#3


0  

Provide tags to all buttons, connect each button actions to same function and try the following code:

为所有按钮提供标签,将每个按钮操作连接到相同的功能并尝试以下代码:

@IBAction func butnClicked (sender : UIButton) {

@IBAction func butnClicked(sender:UIButton){

    for tg in 1...2 {
        print(sender.tag)
        let tmpButton = self.view.viewWithTag(tg) as? UIButton
        if tmpButton?.tag == sender.tag {
          tmpButton?.setTitleColor(.red, for: .normal)
        } else {
           tmpButton?.setTitleColor(.gray, for: .normal)
        }
    }

Hope will be helping.

希望会有所帮助。

#1


4  

Sounds like you want UIControlState.Normal

听起来像你想要UIControlState.Normal

Selected does nothing in most cases, and Highlighted is only while you're pressing the button. See more info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State

在大多数情况下,“选定”不执行任何操作,只有在按下按钮时才会突出显示。在此处查看更多信息:https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State

#2


2  

Maybe you can do with a transform...

也许你可以做一个变换......

@IBAction func buttonPressed(sender: UIButton) {
    sender.titleLabel!.textColor = UIColor.blueColor()
    sender.transform = CGAffineTransformMakeScale(0.8, 0.8)
}

@IBAction func buttonReleased(sender: UIButton) {
    sender.titleLabel!.textColor = UIColor.redColor()
    sender.transform = CGAffineTransformIdentity
}

#3


0  

Provide tags to all buttons, connect each button actions to same function and try the following code:

为所有按钮提供标签,将每个按钮操作连接到相同的功能并尝试以下代码:

@IBAction func butnClicked (sender : UIButton) {

@IBAction func butnClicked(sender:UIButton){

    for tg in 1...2 {
        print(sender.tag)
        let tmpButton = self.view.viewWithTag(tg) as? UIButton
        if tmpButton?.tag == sender.tag {
          tmpButton?.setTitleColor(.red, for: .normal)
        } else {
           tmpButton?.setTitleColor(.gray, for: .normal)
        }
    }

Hope will be helping.

希望会有所帮助。