2012-06-14 11 views
41

की 'विशेषताकृत टेक्स्ट' संपत्ति सेट कर सकता हूं क्या मैं attributedTextUILabel ऑब्जेक्ट की संपत्ति सेट कर सकता हूं? मैं नीचे दिए गए कोड की कोशिश की:क्या मैं `UILabel`

UILabel *label = [[UILabel alloc] init]; 
label.attributedText = @"asdf"; 

लेकिन यह इस त्रुटि देता है:

Property "attributedText" not found on object of type 'UILabel *'

#import <CoreText/CoreText.h> काम नहीं कर रहा

+0

इस लिंक का उपयोग करें http://stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

+0

मुझे लगता है कि यह उपयोगी है के लिए यू http : //stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

उत्तर

31

दुर्भाग्य से, UILabel नहीं करता है जिम्मेदार स्ट्रिंग का समर्थन नहीं करते हैं। आप इसके बजाय OHAttributedLabel का उपयोग कर सकते हैं।

अद्यतन: चूंकि आईओएस 6, UILabel जिम्मेदार तारों का समर्थन करता है। अधिक जानकारी के लिए UILabel reference या माइकल केसलर का उत्तर नीचे देखें।

NSString *redText = @"red text"; 
NSString *greenText = @"green text"; 
NSString *purpleBoldText = @"purple bold text"; 

NSString *text = [NSString stringWithFormat:@"Here are %@, %@ and %@", 
        redText, 
        greenText, 
        purpleBoldText]; 

// If attributed text is supported (iOS6+) 
if ([self.label respondsToSelector:@selector(setAttributedText:)]) { 

    // Define general attributes for the entire text 
    NSDictionary *attribs = @{ 
           NSForegroundColorAttributeName: self.label.textColor, 
           NSFontAttributeName: self.label.font 
           }; 
    NSMutableAttributedString *attributedText = 
     [[NSMutableAttributedString alloc] initWithString:text 
               attributes:attribs]; 

    // Red text attributes 
    UIColor *redColor = [UIColor redColor]; 
    NSRange redTextRange = [text rangeOfString:redText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration 
    [attributedText setAttributes:@{NSForegroundColorAttributeName:redColor} 
          range:redTextRange]; 

    // Green text attributes 
    UIColor *greenColor = [UIColor greenColor]; 
    NSRange greenTextRange = [text rangeOfString:greenText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration 
    [attributedText setAttributes:@{NSForegroundColorAttributeName:greenColor} 
          range:greenTextRange]; 

    // Purple and bold text attributes 
    UIColor *purpleColor = [UIColor purpleColor]; 
    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.label.font.pointSize]; 
    NSRange purpleBoldTextRange = [text rangeOfString:purpleBoldText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration 
    [attributedText setAttributes:@{NSForegroundColorAttributeName:purpleColor, 
            NSFontAttributeName:boldFont} 
          range:purpleBoldTextRange]; 

    self.label.attributedText = attributedText; 
} 
// If attributed text is NOT supported (iOS5-) 
else { 
    self.label.text = text; 
} 
+37

आईओएस 6 के रूप में, UILabel विशेषताकृत टेक्स्ट के माध्यम से जिम्मेदार तारों का समर्थन करता है। – Greg

1

आशा इस मदद करता है,)

NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"asdf"]; 
[attrStr setFont:[UIFont systemFontOfSize:12]]; 
[attrStr setTextColor:[UIColor grayColor]]; 
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)]; 
lbl.attributedText = attrStr; 
+0

बस ऊपर दिए गए कोड का उपयोग करें और इसे काम करना चाहिए। और कुछ करने को नहीं। – Blade

+0

एक त्रुटि देता है: 'UILabel *' – Shamsiddin

+0

प्रकार की वस्तु पर संपत्ति "विशेषताकृत टेक्स्ट" नहीं मिली "CoreText.framework" आयात करने का प्रयास करें। और उसके बाद अपनी .h फ़ाइल #import Blade

196

यहाँ कैसे एक लेबल पर एक जिम्मेदार ठहराया पाठ का उपयोग करने का एक पूरा उदाहरण है

myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()]) 
+1

रेंजऑफस्ट्रिंग का उपयोग करके दिमाग में ध्यान रखें, अगर टेक्स्ट के कुछ हिस्सों में दूसरों का सबसेट होता है तो इससे बग का कारण बन जाएगा। आप स्वयं एनएसआरेंज का उपयोग करके श्रेणियों को काम करने और मैन्युअल रूप से तारों की लंबाई को काम करने के लिए बेहतर काम करेंगे। – owencm

+0

@owencm, आप बिल्कुल सही हैं। इस कोड का उपयोग किसी भी स्थिति में नहीं किया जा सकता है - खासकर जब ग्रंथ वेब से नहीं आते हैं। यह कोड स्निपेट केवल पिछड़ा संगतता के साथ विशेषता वाले टेक्स्ट का उपयोग करने का तरीका दिखाता है ... –

+0

अच्छा उत्तर। लेकिन अरे यह एक _obtuse_ एपीआई है। – aroth

8
NSString *str1 = @"Hi Hello, this is plain text in red"; 

NSString *cardName = @"This is bold text in blue"; 

NSString *text = [NSString stringWithFormat:@"%@\n%@",str1,cardName]; 


// Define general attributes for the entire text 
NSDictionary *attribs = @{ 
          NSForegroundColorAttributeName:[UIColor redColor], 
          NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12] 
          }; 
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs]; 


UIFont *boldFont = [UIFont fontWithName:@"Helvetica-Bold" size:14.0]; 
NSRange range = [text rangeOfString:cardName]; 
[attributedText setAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor], 
           NSFontAttributeName:boldFont} range:range]; 


myLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 

myLabel.attributedText = attributedText; 
5

तेज इस्तेमाल करने वाले लोगों के लिए, यहाँ एक एक लाइनर है::

3
तो

, स्ट्रिंग के उप स्ट्रिंग्स के लिए अलग-अलग गुण होने के लिए कोड यहां दिया गया है।

NSString *[email protected]"10 people likes this"; 
    NSString *[email protected]"likes this"; 
    if ([str hasSuffix:str2]) 
    { 
     NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:str]; 

    // for string 1 // 

     [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,str.length-str2.length)]; 
     [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0,str.length-str2.length)]; 
    // for string 2 // 

     [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange((str.length-str2.length),str2.length)]; 
     [string addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12] range:NSMakeRange((str.length-str2.length),str2.length)]; 
     label.attributedText=string; 
    } 
    else 
    { 
     label.text =str; 

    } 
0
UIFont *font = [UIFont boldSystemFontOfSize:12]; 
NSDictionary *fontDict = [NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName]; 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@" v 1.2.55" attributes: fontDict]; 

UIFont *fontNew = [UIFont boldSystemFontOfSize:17]; 
NSDictionary *fontDictNew = [NSDictionary dictionaryWithObject: fontNew forKey:NSFontAttributeName]; 
NSMutableAttributedString *attrStringNew = [[NSMutableAttributedString alloc] initWithString:@“Application” attributes: fontDictNew]; 
[attrStringNew appendAttributedString: attrString]; 
self.vsersionLabel.attributedText = attrStringNew; 

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