如何更改UIButton标题颜色?

时间:2022-11-21 08:40:41

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 个解决方案

#1


460  

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

你可以使用-[UIButton setTitleColor:forState:]来做这个。

Example:

例子:

Objective-C

objective - c

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

Swift 2

斯威夫特2

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

Swift 3

斯威夫特3

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

Thanks to richardchildan

由于richardchildan

#2


21  

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

你创建的UIButton就是添加了ViewController,下面的实例方法来改变UIFont, tintColor和UIButton的TextColor

Objective-C

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

Swift3

    buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
    buttonName.tintColor = UIColor.purple
    buttonName.setTitleColor(UIColor.purple, for: .normal)

#3


2  

Solution in Swift 3:

迅速解决方案3:

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

This will set the title color of button.

这将设置按钮的标题颜色。

#4


0  

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

如果你正在使用Swift,这也会起到同样的作用:

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

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

Hope that helps!

希望会有帮助!

#5


0  

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

使用Swift 3, UIButton有一个setTitleColor(_:for:)方法。setTitleColor(_:for:)具有以下声明:

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:):

下面的操场代码展示了如何在UIViewController中创建一个UIbutton,并使用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.

选择显示辅助编辑器>时间线 <页面> 的名称。在游乐场中的xcplaygroundpage,以显示UIViewController创建的实例。

#1


460  

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

你可以使用-[UIButton setTitleColor:forState:]来做这个。

Example:

例子:

Objective-C

objective - c

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

Swift 2

斯威夫特2

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

Swift 3

斯威夫特3

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

Thanks to richardchildan

由于richardchildan

#2


21  

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

你创建的UIButton就是添加了ViewController,下面的实例方法来改变UIFont, tintColor和UIButton的TextColor

Objective-C

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

Swift3

    buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
    buttonName.tintColor = UIColor.purple
    buttonName.setTitleColor(UIColor.purple, for: .normal)

#3


2  

Solution in Swift 3:

迅速解决方案3:

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

This will set the title color of button.

这将设置按钮的标题颜色。

#4


0  

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

如果你正在使用Swift,这也会起到同样的作用:

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

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

Hope that helps!

希望会有帮助!

#5


0  

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

使用Swift 3, UIButton有一个setTitleColor(_:for:)方法。setTitleColor(_:for:)具有以下声明:

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:):

下面的操场代码展示了如何在UIViewController中创建一个UIbutton,并使用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.

选择显示辅助编辑器>时间线 <页面> 的名称。在游乐场中的xcplaygroundpage,以显示UIViewController创建的实例。