【问题标题】:How can I change UIButton title color?如何更改 UIButton 标题颜色?
【发布时间】:2011-01-29 06:41:22
【问题描述】:

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

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

如何更改标题颜色?

【问题讨论】:

    标签: ios objective-c iphone uibutton


    【解决方案1】:

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

    例子:

    Objective-C

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

    斯威夫特 2

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

    斯威夫特 3

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

    感谢richardchildan

    【讨论】:

    • 例如 [buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    • 我不敢相信 [UIButton setTitleColor:forState:] 有效,但 btn.titleColor = [UIColor x] 无效..
    • @Lengus 我不知道您如何访问btn.titleColor,只有btn setTitleColor 可用
    • 绝妙的答案。你必须设置状态只是为了改变颜色,这很荒谬...... :(
    • RGB 怎么样?
    【解决方案2】:

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

    Objective-C

     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)
    

    Swift3

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

    【讨论】:

    • 请不要简单地发布代码。提供有关您的代码的一些解释或信息或用法。例如,请参阅this answer
    【解决方案3】:
    Swift 3中的

    解决方案

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

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

    【讨论】:

      【解决方案4】:

      在 Swift 5 中,UIButton 有一个 setTitleColor(_:for:) 方法。 setTitleColor(_:for:) 有以下声明:

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

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

      以下 Playground 示例代码展示了如何在 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: 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
      

      【讨论】:

        【解决方案5】:

        如果您使用的是 Swift,这将执行相同的操作:

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

        希望有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-31
          • 1970-01-01
          • 2015-01-09
          • 1970-01-01
          • 1970-01-01
          • 2022-10-03
          • 2021-11-04
          相关资源
          最近更新 更多