2012-10-08 13 views
6

मैं UILabel या UITextView को बोल्ड और सामान्य टेक्स्ट के अंदर बनाने की कोशिश कर रहा हूं।इसमें बोल्ड और सामान्य टेक्स्ट के साथ UILabel या UITextView कैसे बनाएं?

मैं attributedstring माध्यम से चले गए, लेकिन जब मैं अपने कस्टम सेल के लेबल में इस सेटिंग कर रहा हूँ यह किसी भी पाठ प्रदर्शित नहीं करता है।

मैं भी UITextView setContentToHTMLString: विधि का इस्तेमाल किया है, लेकिन यह गैर-दस्तावेजी है और एप्लिकेशन को अस्वीकार कर दिया मिलता है।

क्या कोई इस के लिए कुछ प्रकार का समाधान दे सकता है?

+0

वाह ... मैंने कभी भी एक प्रश्न "समुदाय विकी" (मिनटों में 9 संशोधन) इतनी तेजी से नहीं देखा है! –

+1

मुझे अभी भी समझ में नहीं आता कि यह सवाल एक समुदाय विकी में क्यों परिवर्तित हुआ .. – Krishnabhadra

उत्तर

5

उपयोग "NSAttributedString" कोई एक लेबल में एकाधिक फ़ॉन्ट पाठ सेट करने के लिए & उपयोग CATextLayer यह रेंडर करने के लिए (एप्पल डेवलपर की बहुत बार दोहराया सुविधा अनुरोधों को सुनने के लिए):

सिर्फ #import "NSAttributedString+Attributes.h"

और उसके बाद इसे लागू इस तरह:

NSString *string1 = @"Hi"; 

NSString *string2 = @"How are you ?"; 

NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1]; 

[attr1 setFont:[UIFont systemFontOfSize:20]]; 

NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2] 

[attr2 setFont:[UIFont boldSystemFontOfSize:20]]; 

[attr1 appendAttributedString:attr2] 

CATextLayer *textLayer = [CATextLayer layer]; 

layer.string = attr1; 

layer.contentsScale = [[UIScreen mainScreen] scale]; 

(Your_text_label).layer = textLayer; 

OR (if you want to render on a view directly) 

[(Your_View_Name).layer addSublayer:textLayer]; 
5

iOS 6.0 तक, आप एक सामान्य UILabel या UITextView के साथ ऐसा नहीं कर सकता है, लेकिन आप NSAttributedString कुछ संभावित खुला स्रोत समाधान के साथ वस्तुओं का उपयोग कर सकते हैं।

TTAttributedLabel या OHAttributedLabel तरह

आईओएस एसडीके, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString में निर्मित एक समाधान।

और, नीचे टिप्पणीकर्ताओं की तरह कहते हैं, हाँ आप "attributedText" property के साथ ऐसा कर सकते हैं। Horray!

+0

कृपया ध्यान दें कि UILabel आईओएस 6 के साथ एनएसएट्रिब्यूटेड स्ट्रिंग के साथ संगत है। – Eiko

+0

आप वास्तव में 'attributedText' प्रॉपर्टी –

+0

का उपयोग करके आईओएस 6 के बाद सामान्य UILabels और UITextView के साथ ऐसा कर सकते हैं, मैं आईओएस 5 और आगे का उपयोग कर रहा हूं, क्या यह आईओएस 5 में समर्थन करेगा? – Rocker

1

मैं जानता हूँ कि यह एक पुरानी धागा है, लेकिन यह कुछ मैं सिर्फ अपने आप को पता चला है। कम से कम एक्सकोड संस्करण 4.6.3 में एक जिम्मेदार टेक्स्ट व्यू का उपयोग करके यह संभव है। इससे भी बेहतर यह है कि इंटरफ़ेस बिल्डर में सभी को किया जाना संभव है!

प्लेस "जिम्मेदार ठहराया" करने के लिए अब आप अपने इच्छित पाठ दर्ज इच्छित स्थान पर अपने TextView TextView का चयन करें और उपयोगिताएँ पैनल के तहत गुण टैब खोल TextView पाठ बदलें:

ये कदम उठाएँ , प्रकाश डाला जो कुछ भी पाठ आप बोल्ड करना चाहते हैं, fontName के बगल में रेखांकित, आदि "टी" बटन पर क्लिक करें पॉपअप में, अपने वांछित टाइपफेस चयन (पूर्व: बोल्ड) आप उपयोगिताएँ पैनल में दिखाया गया है वांछित टाइपफेस देखना चाहिए आनंद लें!

0

यदि आपको इंस्पेक्टर में जिम्मेदार टेक्स्ट संपादित करने में कोई समस्या है, तो टेक्स्ट को एक समृद्ध टेक्स्ट एडिटर में कॉपी/पेस्ट करें, इसे एडजस्ट करें, टेक्स्ट व्यू टेक्स्ट विकल्प को जिम्मेदार और पेस्ट पर स्विच करें। Vwala।

1

निम्नलिखित कोड आईओएस 6.0 और उसके बाद के संस्करण के लिए है। नतीजा यह है कि पाठ "यह बोल्ड है" बोल्ड में होगा और "यह बोल्ड नहीं है।" सामान्य पाठ होगा।

if ([self.registrationLabel respondsToSelector:@selector(setAttributedText:)]) 
{ 
    // iOS6 and above : Use NSAttributedStrings 
    const CGFloat fontSize = 17; 
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; 
    UIFont *regularFont = [UIFont systemFontOfSize:fontSize]; 
    //UIColor *foregroundColor = [UIColor clearColor]; 

    // Create the attributes 
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys: 
           boldFont, NSFontAttributeName, nil]; 
    NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys: 
           regularFont, NSFontAttributeName, nil]; 
    const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded 

    // Create the attributed string (text + attributes) 
    NSString *text = @"This is bold and this is not bold.; 
    NSMutableAttributedString *attributedText = 
    [[NSMutableAttributedString alloc] initWithString:text 
              attributes:subAttrs]; 
    [attributedText setAttributes:attrs range:range]; 

    // Set it in our UILabel and we are done! 
    [self.registrationLabel setAttributedText:attributedText]; 
} 
संबंधित मुद्दे