0

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];
iCodes
  • 1,382
  • 3
  • 21
  • 47

3 Answers3

2

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];
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
  • Okay.....but now I am getting excetion ......Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString attribute:atIndex:effectiveRange:]: unrecognized selector sent to instance 0x9daa5f0' – iCodes Dec 26 '13 at 09:50
  • How are you adding objects in `menuTextArray` ? Make sure you are addin `NSMutableAttributedString` type object into your `menuTextArray` array, as I mentioned in code. – Bhumeshwer katre Dec 26 '13 at 09:53
  • I convert one string and add to array then convert another and again add to array ....[menuTextArray addObject:finalString3]; – iCodes Dec 26 '13 at 09:56
  • Please see my code, how I added `NSMutableAttributedString` type object in array. – Bhumeshwer katre Dec 26 '13 at 09:57
  • I just added the exact solution.....but now I am getting this....*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' – iCodes Dec 26 '13 at 10:08
  • This is exception becuase your array does not contain any objects and you are trying to access object from an empty array. Please update your code. – Bhumeshwer katre Dec 26 '13 at 10:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43907/discussion-between-icodes-and-bhumeshwer-katre) – iCodes Dec 26 '13 at 10:12
1

Use attributedText in place of text

cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
san
  • 3,350
  • 1
  • 28
  • 40
0

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];
Vidhyanand
  • 993
  • 10
  • 21