简体   繁体   English

Swift - 轻触UIButton时更改文本的颜色和大小

[英]Swift - Change color and size of text when UIButton is tapped

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. 我试过.Highlighted和。在UIControlState上选择,似乎都不起作用。 I have also tried the following, but I cant get it to work. 我也试过以下,但我不能让它工作。

@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. 我认为既然发件人是UIButton,那就是点击的按钮,取下它的值并重置它们就行了。 I do believe I am missing something. 我相信我错过了一些东西。

Thank you 谢谢

Sounds like you want UIControlState.Normal 听起来像你想要UIControlState.Normal

Selected does nothing in most cases, and Highlighted is only while you're pressing the button. 在大多数情况下,“ Selected不执行任何操作,只有在按下按钮时才会Highlighted 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

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
}

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. 希望会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM