2013-05-08 7 views
5

में अलग अलग रंग और फ़ॉन्ट प्रकार प्रदर्शित करने के लिए है की तरह,मैं एक स्ट्रिंग कैसे एक UILabel

NSSting *[email protected]"First Second Third Fourth Fifth Sixth Seventh"; 

मैं भिन्न फ़ॉन्ट प्रकार और रंग के साथ एक लेबल में इस स्ट्रिंग प्रदर्शित करना चाहते हैं।

यानी ..,

"सबसे पहले" स्ट्रिंग बोल्ड में होना चाहिए।

"दूसरा" स्ट्रिंग इटैलिक में होना चाहिए।

"तीसरा" स्ट्रिंग लाल रंग में होना चाहिए।

"चौथा" स्ट्रिंग हरे रंग के रंग में होना चाहिए।

"पांचवां" स्ट्रिंग सिस्टम फ़ॉन्ट के बोल्ड इटालिक में होना चाहिए।

"छठी" स्ट्रिंग एरियल बोल्ड होना चाहिए।

"सातवीं" स्ट्रिंग आकार 34.

मैं stackoverflow में कुछ उत्तर के माध्यम से चले गए हैं, लेकिन मैं उन सवालों के जवाब में कोई स्पष्टता नहीं मिला ..

कर सकते हैं किसी भी एरियल बोल्ड होना चाहिए एक कुछ दिशानिर्देश प्रदान करते हैं।

अग्रिम में धन्यवाद।

समाधान: अपनी आवश्यकता के अनुसार सबसे पहले NSAttributedString का उपयोग करें।

NSAttributedString का उपयोग करके आप iOS 6 UILabel में attributedString property कर सकते हैं।

Below ios 6.0TTAtributeLabel या displaying attributedString के लिए third party attributedLabel का उपयोग करें।
@property(nonatomic,copy) NSAttributedString *attributedText
कि एक NSAttributedString स्वीकार करता है:

+0

आप इसे एकल लेबल में नहीं कर सकते हैं। NSAttributedString का उपयोग करते हुए आपको 7 अलग-अलग लेबल –

+1

की आवश्यकता होगी, आप इसे ios6 uilabel में कर सकते हैं विशेषता संपत्ति को श्रेय दिया है। –

+0

आईओएस 6.0 के नीचे विशेषता वितरित करने के लिए TTAtributeLabel का उपयोग करें। –

उत्तर

1

आईओएस 6.0 में और UILabel ऊपर एक संपत्ति है।

आप NSAttributedString बना सकते हैं जो आपकी आवश्यकताओं से मेल खाता है।

+0

आईओएस 6.0 से कम के लिए क्या ???? –

0

कोशिश करो ....

DAAttributedStringUtils

और यह भी देखने this

Different Label

आशा है कि मैं

+0

यह जांचें http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html –

+0

@ प्रिंस: आप क्या कहते हैं? कृपया विस्तार से समझाएं –

3

अलग शब्द के लिए सेट अलग रंग के लिए इस कोड का प्रयोग करें मदद की।

NSString *test = @"First Second Third Fourth Fifth Sixth Seventh"; 

CFStringRef string = (CFStringRef) test; 
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string); 



    /* 
    Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its 
    attributes right when we create it. We can change them if we use mutable form of CFAttributeString. 
    */ 



    //Lets choose the colors we want to have in our string 
    CGColorRef _orange=[UIColor orangeColor].CGColor; 
    CGColorRef _green=[UIColor greenColor].CGColor; 
    CGColorRef _red=[UIColor redColor].CGColor; 
    CGColorRef _blue=[UIColor blueColor].CGColor;  




    //Lets have our string with first 20 letters as orange 
    //next 20 letters as green 
    //next 20 as red 
    //last remaining as blue 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);  
    CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue); 

इसके अलावा आप छवि के साथ कलर जैसे रंग सेट कर सकते हैं ..

[yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]]; 

देखें से This Link

मैं आशा है कि यह आप के लिए उपयोगी है मेरे एक और उत्तर ...

1

यदि u नहीं पर मेरे लिए काम किया है NSAttributeLabel का उपयोग करें इसे आजमाएं, यह मेरे लिए ठीक काम करता है

इस ढांचे CoreText.framework

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName 
{ 
    label.layer.sublayers = nil; 

    NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text]; 

    for (int i =0 ; i<[rangeString count]; i++) 
    { 
     CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].pointSize, NULL); 

     NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]]; 

     if (whiteRange.location != NSNotFound) 
     { 
      [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange]; 
      [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName 
              value:(__bridge id)ctFont 
              range:whiteRange]; 
     } 
    } 

    CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap]; 

    CATextLayer *textLayer = [[CATextLayer alloc]init]; 
    textLayer.frame =CGRectMake(0,0, label.frame.size.width,expectedLabelSize.height+4); 
    textLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    textLayer.string=mutableAttributedString; 
    textLayer.opacity = 1.0; 
    textLayer.alignmentMode = kCAAlignmentLeft; 
    [label.layer addSublayer:textLayer]; 
    [textLayer setWrapped:TRUE]; 

    //label.lineBreakMode = UILineBreakModeWordWrap; 

    label.text = @""; 
} 

और इस प्रकार इस विधि कॉल के बाद CommonFuction वर्ग & में इस विधि जोड़ने: -

[CommonFunctions setMultiColorAndFontText:string rangeString:[NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6, nil] label:yourLabel font:[NSArray arrayWithObjects:@"Arial",@"Arial",@"Arial",@"Arial",@"Arial",@"Arial"nil] color:[NSArray arrayWithObjects:(UIColor *)[UIColor colorWithRed:(0/255.f) green:(167/255.f) blue:(230/255.f) alpha:1.0f].CGColor,(UIColor *)[UIColor colorWithRed:(189/255.f) green:(189/255.f) blue:(189/255.f) alpha:1.0f].CGColor, color3,color4,color5,color6,nil]]; 

मैं आशा है कि यह होगा यू

के लिए ठीक काम करता है

`

0

मुझे विकास के दौरान एक ही समस्या का सामना करना पड़ा है .since NSAttributeString ऊपर ioS6 n से ऊपर काम करता है। तीसरे पक्ष के वर्ग प्राप्त करने के बजाय, मैं आपको कस्टम UILabel बनाने का सुझाव देता हूं जो आपके लेबल को अनुकूलित करने के लिए समर्थन करता है संक्षेप में, रंग, बोल्ड, लंबाई निर्दिष्ट करके कई लेबल (शब्दों द्वारा शब्दों) बनाएं। और सभी लेबल एक साथ जोड़ें और एक नया लेबल ऑब्जेक्ट बनाएं

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