2010-03-19 15 views
157

मैं एक बटन प्रोग्राम के रूप में ..........मैं 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]; 

मैं शीर्षक रंग कैसे बदल सकते हैं बनाने के?

उत्तर

441

आप ऐसा करने के लिए -[UIButton setTitleColor:forState:] का उपयोग कर सकते हैं।

उदाहरण:

ऑब्जेक्टिव-सी

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

स्विफ्ट 2

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

स्विफ्ट 3

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

richardchildan

+36

उदाहरण के लिए [बटन नाम सेट टिटल रंग: [यूआईसीओलर ब्लैककॉलर] फॉरस्टेट: यूआईसींट्रोलस्टेट नॉर्मल]; –

+1

@dkberktas हमारे सुझाव के लिए धन्यवाद। मैं इसे काम करता हूं। – ram

+3

मैं विश्वास नहीं कर सकता [UIButton setTitleColor: forState:] काम करता है लेकिन btn.titleColor = [UIColor x] नहीं .. – Legnus

21

के लिए धन्यवाद आप बनाया UIButton ViewController, UIFont, tintColor और UIButton

ऑब्जेक्टिव-सी

buttonName.titleLabel.font = [UIFont fontWithName:@"LuzSans-Book" size:15]; 
buttonName.tintColor = [UIColor purpleColor]; 
[buttonName setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 

की textcolor बदलने के लिए निम्नलिखित उदाहरण विधि जोड़ा जाता है स्विफ्ट

मदद करता है कि

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

आशा:

Swift3

buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15) 
    buttonName.tintColor = UIColor.purple 
    buttonName.setTitleColor(UIColor.purple, for: .normal) 
+2

कृपया कोड पोस्ट न करें। कुछ स्पष्टीकरण या जानकारी या उपयोग करें कोड। उदाहरण के लिए, [यह उत्तर] देखें (http://stackoverflow.com/a/16893057/756941)। – NAZIK

0

आप स्विफ्ट का उपयोग कर रहे हैं, तो यह एक ही करना होगा!

0

स्विफ्ट 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 खेल का मैदान में उदाहरण।

2

समाधानमें स्विफ्ट 3:

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

यह बटन के शीर्षक रंग सेट हो जाएगा।

संबंधित मुद्दे