2011-05-03 20 views
6

मेरे पास एक यूआई बटन है जिसे मैं उस पर दो लेबल रखना चाहता हूं, इस तरह कि सेल में शीर्षक टेक्स्ट और विवरण टेक्स्ट कैसा है।एकाधिक लेबल के साथ आईओएस UIButton

मैं बटन को मुख्य टेक्स्ट के लिए एक बड़ा फ़ॉन्ट रखना चाहता हूं, और इसके तहत छोटे विवरण टेक्स्ट हैं।

क्या यह संभव है? मैंने बटन पर कई लाइनें डालने की कोशिश की है, लेकिन मुझे प्रत्येक पंक्ति के लिए अलग-अलग टेक्स्ट आकारों की आवश्यकता है, इसलिए लाइनब्रेकोड और शीर्षक की संख्या सेट करना लेबल लेबल वास्तव में काफी काम नहीं करता है।

उत्तर

6

यहां कोड का अंततः उपयोग किया जा सकता है। जॉन वांग से सहायता।

सुझावों के लिए सभी को धन्यवाद!

// Formats a label to add to a button. Supports multiline buttons 
// Parameters: 
// button - the button to add the label to 
// height - height of the label. usual value is 44 
// offset - the offset from the top of the button 
// labelText - the text for the label 
// color - color of the text 
// formatAsBold - YES = bold NO = normal weight 
// tagNumber - tag for the label 

- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber { 

    // Get width of button 
    double buttonWidth= button.frame.size.width; 

    // Initialize buttonLabel 
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)]; 

    // Set font size and weight of label 
    if (formatAsBold) { 
     buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize]; 
    } 
    else { 
     buttonLabel.font = [UIFont systemFontOfSize:fontSize]; 
    } 

    // set font color of label 
    buttonLabel.textColor = color; 

    // Set background color, text, tag, and font 
    buttonLabel.backgroundColor = [UIColor clearColor]; 
    buttonLabel.text = labelText; 
    buttonLabel.tag = tagNumber; 

    // Center label 
    buttonLabel.textAlignment = UITextAlignmentCenter; 

    // Add label to button 
    [button addSubview:buttonLabel]; 

    [buttonLabel autorelease]; 
} // End formatLabelForButton 
4

एक चाल जो मैं अनुशंसा करता हूं वह यूआईएलएबल्स के ऊपर एक पारदर्शी इंटीरियर के साथ यूआईबटन डाल रहा है। मैंने पहले इस चाल का उपयोग किया है और, हालांकि यह रखरखाव और i18n के मामले में कुछ समस्याएं पेश कर सकता है, यह एक आकर्षण की तरह काम करता है।

ऊपर दिए गए सुझाव का उपयोग करके यहां 5 मिनट का नमूना है। Label behind button

अधिक समय दिया गया, आप गोल कोनों के साथ एक बेहतर लेबल बना सकते हैं।

+0

सुझाव के लिए धन्यवाद, लेकिन मुझे बटन के लिए पृष्ठभूमि के साथ एक दृश्य बनाने की आवश्यकता होगी, और मैं आदर्श रूप से बटन पर दो अलग-अलग फ़ॉन्ट शैलियों को रखना चाहता हूं। – orangemako

+0

@ ऑरेंजेमाको जब आप @ गॉर्डन सुझाव का उपयोग करते हैं, तो लेबल पृष्ठभूमि को एक अलग रंग दें। –

1

आपको इसमें सबव्यूव्यू जोड़ने में सक्षम होना चाहिए। चूंकि सबकुछ एक दृश्य है, इसलिए सबकुछ संभावित रूप से सबव्यूज़ हो सकता है।

मैं इसे सबक्लास कर दूंगा और लेबल को उप-वर्ग के भीतर रखूंगा, फिर आप टेक्स्ट और सबटेक्स्ट के गुणों को उनके मूल्यों को बदलने के लिए बढ़ा सकते हैं।

यह नहीं कह रहा कि यह 100% काम कर सकता है। लेकिन मेरे सिर के ऊपर से। UIView में सबव्यूज़

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