2010-03-19 51 views
157

creo un botón de programación ..........¿Cómo puedo cambiar el color del título de 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]; 

cómo puedo cambiar el color del título?

Respuesta

441

Puede usar -[UIButton setTitleColor:forState:] para hacerlo.

Ejemplo:

Objective-C

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

Swift 2

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

Swift 3

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

Gracias a richardchildan

+36

Por ejemplo [buttonName setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; –

+1

@dkberktas Gracias por su sugerencia. Lo uso funciona – ram

+3

No puedo creer [UIButton setTitleColor: forState:] funciona pero btn.titleColor = [UIColor x] no ... – Legnus

21

ha creado el UIButton se añade el ViewController, el siguiente método de instancia para cambiar UIFont, tintColor y 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

Por favor, no publique simplemente el código. Proporcione alguna explicación o información o uso sobre su código. Por ejemplo, vea [esta respuesta] (http://stackoverflow.com/a/16893057/756941). – NAZIK

0

Si está utilizando Swift, esto va a hacer lo mismo:

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

Espero que ayude!

0

Con Swift 3, UIButton tiene un método setTitleColor(_:for:). setTitleColor(_:for:) tiene la siguiente declaración:

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

Establece el color del título que se utilizará para el estado especificado.


El siguiente código muestran juegos de cómo crear un UIbutton en un UIViewController y cambiar su color del título utilizando 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 

Seleccionar Show the Assistant Editor>Timeline <Name of the page>.xcplaygroundpage en patio con el fin de mostrar la UIViewController creado ejemplo.

2

Solución en Swift 3:

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

Esto fijará el color del título del botón.

Cuestiones relacionadas