Как изменить цвет заголовка UIButton?

Я создаю кнопку программно..........

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

Как изменить цвет заголовка?

Ответ 1

Вы можете использовать -[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

Ответ 2

Вы создали UIButton, добавлен ViewController, следующий метод экземпляра для изменения UIFont, tintColor и TextColor UIButton

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)

Ответ 3

Решение в Swift 3:

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

Это установит цвет названия кнопки.

Ответ 4

Если вы используете Swift, это будет делать то же самое:

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

Надеюсь, что это поможет!

Ответ 5

В Swift 5 UIButton имеет setTitleColor(_:for:). setTitleColor(_:for:) имеет следующее объявление:

Устанавливает цвет заголовка для использования в указанном состоянии.

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

В следующем примере кода Playground показано, как создать UIbutton в 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 attributes
        button.setTitle("Print 0", for: UIControl.State.normal)
        button.setTitleColor(UIColor.orange, for: UIControl.State.normal)

        // Set button 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