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.
fonte
2017-02-18 11:54:52
Ad esempio [buttonName setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; –
@dkberktas Grazie per il suggerimento. io lo uso funziona. – ram
Non posso credere [UIButton setTitleColor: forState:] funziona ma btn.titleColor = [UIColor x] no .. – Legnus