Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of text when button is pushed?

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:

@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

    }
like image 451
blue Avatar asked Jul 18 '14 17:07

blue


People also ask

How do you color button text?

Use a semi-colon to separate the different style elements in the HTML button tag. Type color: in the quotation marks after "style=". This element is used to change the text color in the button. You can place style elements in any order in the quotation markers after "style=".

How do you change the color of a button using Javascript?

</head> <body> <input type="button" onmouseover="ChangeColor()" value="Button" id="btn1" />


2 Answers

There is no property setter color in UIButton. use instead setTitleColor.Write this in viewWillAppear

colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)

This will change title color to brown for UIControlState.Normal

To set the color of title when button is in Highlighted state use

colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)
like image 115
codester Avatar answered Oct 07 '22 14:10

codester


Swift 3.0 example:

    colorsBtn.setTitleColor(UIColor .white, for: UIControlState.normal)
    colorsBtn.setTitleColor(UIColor .black, for: UIControlState.highlighted)
like image 44
mobilecat Avatar answered Oct 07 '22 13:10

mobilecat