Charles
Charles

Reputation: 4538

Change Text Color From NSAttributedString When UITableViewCell is Selected/Highlighted

I want to be able to change the text in my UITableView cells when they are tapped on (selected/highlighted). The content from each cell is derived from an NSAttributedString. Here is that code:

-(NSAttributedString*)formattedSubject:(int)state {
    if(formattedSubject!=nil) return formattedSubject;
    NSDictionary *boldStyle = [[NSDictionary alloc] init];
    if(state==1) {
        boldStyle = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor colorWithRed:0.067 green:0.129 blue:0.216 alpha:1.0]};
    }
    else {
        boldStyle = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor whiteColor]};
    }
    NSDictionary* normalStyle = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:14.0]};
    NSMutableAttributedString* articleAbstract = [[NSMutableAttributedString alloc] initWithString:subject];
    [articleAbstract setAttributes:boldStyle range:NSMakeRange(0, subject.length)];
    [articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    int startIndex = [articleAbstract length];
    NSTimeInterval _interval=[datestamp doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"MM/dd/yy"];
    NSString* description = [NSString stringWithFormat:@"By %@ on %@",author,[_formatter stringFromDate:date]];
    [articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString: description]];
    [articleAbstract setAttributes:normalStyle range:NSMakeRange(startIndex, articleAbstract.length - startIndex)];
    formattedSubject = articleAbstract;
    return formattedSubject;
}

As you can see, I would like the boldStyle text to be a certain color (given by [UIColor colorWithRed:0.067 green:0.129 blue:0.216 alpha:1.0]) when the state is "1", and white otherwise. I've currently tried the following modifications in cellForRowAtIndexPath, as well as didSelectRowAtIndexPath:

cellForRowAtIndexPath:

NSIndexPath *path = [tableView indexPathForSelectedRow];
if(path) {
    cell.textLabel.attributedText = [news formattedSubject:1];
}
else {
    cell.textLabel.attributedText = [news formattedSubject:0];
}

What I am doing here is checking if a cell is selected, and if so then I changed that cell's "attributedText" to the "0" format. However, this didn't work, and I suspected it was because the modification had to be made when the cell was actually selected. So I tried the following in didSelectRowAtIndexPath:

didSelectRowAtIndexPath:

News *news = newsArray[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.attributedText = [news formattedSubject:0];

However, it seems like the change is never made. I put an NSLog in the "else" part of the boldStyle if/else statement, and it was never written to the console.

Neither of my attempts at a solution have worked, and I've run out of ideas. How can I get the NSAttributedString to change when a cell is highlighted?

Update: It's also interesting that normalStyle text, which is by default black, does turn white when the cell is highlighted/selected when I add cell.textLabel.highlightedTextColor = [UIColor whiteColor]; in cellForRowAtIndexPath, but boldStyle text does not.

Upvotes: 0

Views: 921

Answers (2)

Vinh Huynh
Vinh Huynh

Reputation: 151

I have problem the same, and..

cell.textLabel.enable = NO;

It just resolved

Upvotes: 1

Maximilian Liesegang
Maximilian Liesegang

Reputation: 994

In your cellForRowAtIndexPath method you are checking if a selectedIndexPath in this tableView exists.

NSIndexPath *path = [tableView indexPathForSelectedRow];
if(path) {

I think you want to compare your indexPath and your selectedIndexPath. You should use isEqual: for that.

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
if([selectedIndexPath isEqual:indexPath]) {

Upvotes: 0

Related Questions