How can I change UIButton title color?

0 votes
asked Mar 19, 2010 by helloworld

I create a button programmatically..........

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

how can I change title color?

5 Answers

0 votes
answered Mar 19, 2010 by ben-gottlieb

You can use -[UIButton setTitleColor:forState:] to do this.

Example:

Objective-C

[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

Swift 2

buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)

Swift 3

buttonName.setTitleColor(UIColor.white, for: .normal)

Thanks to richardchildan

0 votes
answered Mar 20, 2013 by anbu-karthik

You created the UIButton is added the ViewController, The following instance method to change UIFont, tintColor and TextColor of the UIButton

Objective-C

 buttonName.titleLabel.font = [UIFont fontWithName:@"LuzSans-Book" size:15];
 buttonName.tintColor = [UIColor purpleColor];
 [buttonName setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];

Swift

buttonName.titleLabel.font = UIFont(name: "LuzSans-Book", size: 15)
buttonName.tintColor = UIColor.purpleColor()
buttonName.setTitleColor(UIColor.purpleColor(), forState: .Normal)

Swift3

    buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
    buttonName.tintColor = UIColor.purple
    buttonName.setTitleColor(UIColor.purple, for: .normal)
0 votes
answered Mar 19, 2016 by fagiani

If you are using Swift, this will do the same:

buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)

Hope that helps!

0 votes
answered Sep 15, 2017 by rupom

Solution in Swift 3:

button.setTitleColor(UIColor.red, for: .normal)

This will set the title color of button.

0 votes
answered Sep 15, 2017 by imanou-petit

With Swift 3, UIButton has a setTitleColor(_:for:) method. setTitleColor(_:for:) has the following declaration:

func setTitleColor(_ color: UIColor?, for state: UIControlState)

Sets the color of the title to use for the specified state.


The following Playground code show how to create a UIbutton in a UIViewController and change it's title color using setTitleColor(_:for:):

import UIKit
import PlaygroundSupport

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white

        // Create button
        let button = UIButton(type: UIButtonType.system)

        // Set button's attributes
        button.setTitle("Print 0", for: UIControlState.normal)
        button.setTitleColor(UIColor.orange, for: UIControlState.normal)

        // Set button's frame
        button.frame.origin = CGPoint(x: 100, y: 100)
        button.sizeToFit()

        // Add action to button
        button.addTarget(self, action: #selector(printZero(_:)), for: UIControlEvents.touchUpInside)

        // Add button to subView
        view.addSubview(button)
    }

    func printZero(_ sender: UIButton) {
        print("0")
    }

}

let controller = ViewController()
PlaygroundPage.current.liveView = controller

Select Show the Assistant Editor > Timeline <Name of the page>.xcplaygroundpage in Playground in order to display the UIViewController created instance.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...