स्विफ्ट 3, UIButton
के साथ setTitleColor(_:for:)
विधि है।
func setTitleColor(_ color: UIColor?, for state: UIControlState)
शीर्षक का रंग सेट करता है निर्दिष्ट राज्य के लिए उपयोग करने के लिए: setTitleColor(_:for:)
निम्नलिखित घोषणा की है।
निम्नलिखित खेल का मैदान कोड शो कैसे एक UIViewController
में एक UIbutton
बना सकते हैं और बदलने के लिए यह शीर्षक setTitleColor(_:for:)
का उपयोग कर रंग है: आदेश UIViewController
बनाया प्रदर्शित करने के लिए
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
करें Show the Assistant Editor
>Timeline <Name of the page>.xcplaygroundpage
खेल का मैदान में उदाहरण।
स्रोत
2017-02-18 11:54:52
उदाहरण के लिए [बटन नाम सेट टिटल रंग: [यूआईसीओलर ब्लैककॉलर] फॉरस्टेट: यूआईसींट्रोलस्टेट नॉर्मल]; –
@dkberktas हमारे सुझाव के लिए धन्यवाद। मैं इसे काम करता हूं। – ram
मैं विश्वास नहीं कर सकता [UIButton setTitleColor: forState:] काम करता है लेकिन btn.titleColor = [UIColor x] नहीं .. – Legnus