2010-03-19 17 views
157

Ho creare un pulsante di programmazione ..........Come posso cambiare il colore del titolo 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]; 

come posso cambiare il colore del titolo?

risposta

441

È possibile utilizzare -[UIButton setTitleColor:forState:] per eseguire questa operazione.

Esempio:

Objective-C

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

Swift 2

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

Swift 3

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

Grazie a richardchildan

+36

Ad esempio [buttonName setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; –

+1

@dkberktas Grazie per il suggerimento. io lo uso funziona. – ram

+3

Non posso credere [UIButton setTitleColor: forState:] funziona ma btn.titleColor = [UIColor x] no .. – Legnus

21

è stato creato l'UIButton è aggiunto il ViewController, il seguente metodo di esempio per cambiare UIFont, tintColor e TextColor del UIButton

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) 
+2

Si prega di non pubblicare semplicemente il codice. Dare qualche spiegazione o informazioni o utilizzo sul vostro codice. Ad esempio, vedere [questa risposta] (http://stackoverflow.com/a/16893057/756941). – NAZIK

0

Se si utilizza Swift, questo farà lo stesso:

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

Speranza che aiuta!

0

Con Swift 3, UIButton ha un metodo setTitleColor(_:for:). setTitleColor(_:for:) ha la seguente dichiarazione:

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

Imposta il colore del titolo da utilizzare per lo stato specificato.


Il seguente spettacolo codice di giochi come creare un UIbutton in un UIViewController e cambiare il suo colore titolo utilizzando 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 

Selezionare Show the Assistant Editor>Timeline <Name of the page>.xcplaygroundpage in parco giochi, al fine di visualizzare il UIViewController creato esempio.

2

Solution in Swift 3:

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

Questo imposterà il colore titolo di tasto.

Problemi correlati