iCodes
iCodes

Reputation: 1457

How to set multiple color text in table view in ios

I have a string ... Share Pictures .I want to show the Share in different color.I used NSMutableAttributedString to change the color of that part of the string.But when I am setting the cell.textLabel.text using the following string it doesn't work.Any other way to do this?

This way it doesn't work.

NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:@"Share pictures "];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
NSString *tempStr3 = @"with your friends.";
NSString *finalString3 = [NSString stringWithFormat:@"%@%@" , string3, tempStr3];
[menuTextArray addObject:finalString3];

And in table view datasource method.

 cell.textLabel.text = [menuTextArray objectAtIndex:indexPath.row];

Upvotes: 0

Views: 1081

Answers (3)

Vidhyanand
Vidhyanand

Reputation: 993

Hope this code helps you.....

    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:@"Share pictures with your friends"];
    [string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 5)];
    [menuTextArray addObject:string3];
    [cell.textLabel setAttributedText:string3];

Upvotes: 0

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

You need to add only NSMutableAttributedString into your menuTextArray:

NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc]initWithString:@"Share pictures with your friends"];
[yourString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
[menuTextArray addObject:yourString];

then set attributedText

cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];

Upvotes: 2

san
san

Reputation: 3328

Use attributedText in place of text

cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];

Upvotes: 1

Related Questions