如何更改UIButton标题颜色?


189

我以编程方式创建一个按钮。

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];

如何更改标题颜色?

Answers:


522

您可以-[UIButton setTitleColor:forState:]用来执行此操作。

例:

物镜

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

迅捷2

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

迅捷3

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

感谢richardchildan


38
例如[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
罗伯特·柴尔德

3
我不能相信[UIButton的setTitleColor:forState:]的作品,但btn.titleColor =的UIColor X]不..
Legnus

@Lengus我看不到如何访问btn.titleColor,这里只有btn setTitleColor可用
-Alex Cio

1
极好的答案。它太荒谬了,您如何设置状态只是为了改变颜色... :(
Supertecnoboff 2015年

RGB怎么样?
Umit Kaya

23

您创建了 UIButton添加了ViewController,下面的实例方法的变化UIFonttintColorTextColorUIButton

物镜

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

迅速

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

迅捷3

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

3
请不要简单地发布代码。提供一些有关您的代码的解释或信息或用法。例如,请参阅此答案
阿西克·阿卜杜拉

3

解决方案斯威夫特3

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

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


1

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

设置标题的颜色以用于指定状态。

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

以下Playground示例代码展示了如何在中创建UIbuttona UIViewController并使用更改其标题颜色setTitleColor(_:for:)

import UIKit
import PlaygroundSupport

class ViewController: UIViewController {

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

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

        // Set button's attributes
        button.setTitle("Print 0", for: UIControl.State.normal)
        button.setTitleColor(UIColor.orange, for: UIControl.State.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: UIControl.Event.touchUpInside)

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

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

}

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

0

如果您使用的是Swift,则将执行以下操作:

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

希望有帮助!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.