5

I have a String Like,

NSSting *myString=@"First Second Third Fourth Fifth Sixth Seventh";

I want to display this string in single label with different font types and colors.

i.e..,

"First" string should be in Bold.

"Second" string should be in italic.

"Third" string should be in red color.

"Fourth" string should be in green color.

"Fifth" string should be in Bold Italic of System font.

"Sixth" String should be arial bold.

"Seventh" String should be arial bold of size 34.

I have gone through some of the answers in stackoverflow, but i didn't get any clarity in those answers..

Can any one provide some guidelines.

Thanks in Advance.

Solution : Firstly manipulate NSAttributedString according to your requirement.

using NSAttributedString u can do it in iOS 6 UILabel has attributedString property.

Below ios 6.0 use TTAtributeLabel or any third party attributedLabel for displaying attributedString.

4
  • You cant do it in single label. 7 different labels you'l need May 8, 2013 at 11:28
  • 1
    using NSAttributedString u can do it in ios6 uilabel has attributedString property. May 8, 2013 at 11:30
  • Below ios 6.0 use TTAtributeLabel to display attributedString. May 8, 2013 at 11:31
  • A number of Question i found on SO google.co.in/…
    – Buntylm
    May 8, 2013 at 11:33

6 Answers 6

3

Use this code for set different color to different word..

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);

Also you can set Color with Image like bellow..

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

See My Another Answer From This Link

0
2

TTTAttributedLabel did the job for me on iOS < 6.

1

In iOS 6.0 and above UILabel has a property:
@property(nonatomic,copy) NSAttributedString *attributedText
that accepts an NSAttributedString.

You can create a NSAttributedString that matches your requirements.

1
  • what for less than iOS 6.0 ???? May 8, 2013 at 11:40
1

if u not use NSAttributeLabel then try this, its works fine for me

add this method into CommonFuction class & add this framework 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 = @"";
}

and after call this method as follow :-

[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

I have faced the same issue while development.since NSAttributeString works above ioS6 n above.Rather than getting third party classes, I suggest you to create custom UILabel which supports to customize your label In brief,create many labels(words by words) by assigning colors,bold,length . And append all label together and create a new Label object

0

Try:

DAAttributedStringUtils

and also see this

Different Label

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.