2
-(UITableViewCell*) makeLikeCell
 {
    // Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle]      loadNibNamed:@"SpotlightLikesTableViewCell" owner: self options: nil];

// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
UITableViewCell *cell = [topLevelObjects objectAtIndex:0];


NSString *likeText = [likesAndComments objectAtIndex: kLikeRow];

TTTAttributedLabel *likeLabel = [[[TTTAttributedLabel alloc] initWithFrame:CGRectZero] autorelease];
likeLabel = (TTTAttributedLabel*) [cell viewWithTag: kCaptionTag];
likeLabel.textColor = [UIColor blueColor];
likeLabel.lineBreakMode = UILineBreakModeWordWrap;
[likeLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 10]];

[likeLabel setText: likeText afterInheritingLabelAttributesAndConfiguringWithBlock: ^NSAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange colorRange = [[mutableAttributedString string] rangeOfString: @" like this photo." options: NSCaseInsensitiveSearch];

    [mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:colorRange];
    return mutableAttributedString; }];

return cell;
}

I added the TTT attributed label to my project. I want to have 2 different font colors to display the "likes". However my project keeps crashing. A sample string to be formatted would be: "andrew, john, amos likes this photo."

any suggestions? enter image description here

anc1revv
  • 1,563
  • 7
  • 24
  • 42
  • Could you be more specific in what the console spits out when it crashes? If you run with breakpoints on, can you do `bt` to get a backtrace after it crashes? My first thought was that you might be running into problems by passing `colorRange` into `-addAttribute:value:range` without checking if the string wasn't found (`colorRange.location == NSNotFound`) – mattt Jun 02 '11 at 21:11

2 Answers2

5

Use like this

[likeLabel setText:likeText afterInheritingLabelAttributesAndConfiguringWithBlock: ^(NSMutableAttributedString *mutableAttributedString) {
    NSRange colorRange = [likeText rangeOfString: @"like this photo." options: NSCaseInsensitiveSearch];
    if (colorRange.location != NSNotFound) {
        [mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:colorRange];
    }
    return mutableAttributedString; 
}];
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • Hi @inder can i use [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[[TEXT_LINK_COLOR] CGColor] range:whiteRange]; Where Text Link color is a constant i have taken in one .h file – BhavikKama Feb 19 '14 at 07:18
0

There is a way to set different / multiple fonts & other properties like color on Label using NSMutableAttributedStrin. Refer to my answer on Multiple Font & Color for string & Display the string on Label

Community
  • 1
  • 1
Akshay
  • 1,341
  • 2
  • 9
  • 18