Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I do believe I am missing something.

Thank you

like image 639
Nate4436271 Avatar asked Dec 04 '14 16:12

Nate4436271


1 Answers

Sounds like you want 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

like image 180
shim Avatar answered Oct 05 '22 02:10

shim