首页 文章

如何更改UIButton Headers 颜色?

提问于
浏览
173

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

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

如何更改 Headers 颜色?

5 回答

  • 478

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

    例:

    Objective-C

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

    Swift 2

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

    Swift 3

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

    感谢richardchildan

  • 0

    你创建的 UIButton 添加了 ViewController ,以下实例方法改变了 UIFonttintColorTextColorUIButton

    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

    Solution in Swift 3

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

    这将设置按钮的 Headers 颜色 .

  • 22

    如果您使用Swift,这将做同样的事情:

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

    希望有所帮助!

  • 2

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

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

    设置要用于指定状态的 Headers 的颜色 .


    以下Playground代码显示如何在 UIViewController 中创建 UIbutton 并使用 setTitleColor(_:for:) 更改其 Headers 颜色:

    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
    

    在Playground中选择 Show the Assistant Editor > Timeline <Name of the page>.xcplaygroundpage 以显示 UIViewController 已创建的实例 .

相关问题