Z Swift 3, UIButton
ma metodę setTitleColor(_:for:)
. setTitleColor(_:for:)
ma następującą deklarację:
func setTitleColor(_ color: UIColor?, for state: UIControlState)
ustawia kolor tytułu korzystania przez określony stan.
Poniższy kod zabaw pokazać, jak stworzyć UIbutton
w UIViewController
i zmienić jego tytuł kolor używając 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
Wybierz Show the Assistant Editor
>Timeline <Name of the page>.xcplaygroundpage
na placu zabaw w celu wyświetlenia UIViewController
utworzony instancja.
Na przykład [buttonName setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; –
@dkberktas Dziękujemy za sugestię. Używam go działa. – ram
Nie mogę uwierzyć [UIButton setTitleColor: forState:] działa, ale btn.titleColor = [UIColor x] nie działa. – Legnus