1

I would like to make a simple change in my tableView. I have a custom table view background and I would like to change the Header and Footer TEXT color,

ONLY the text color.

I've seen on internet that usually we must use the delegate method to give to the table view an entire new view but I would ONLY to change the text color (and, if possibile, the shadow)...

Is there a simple way to do this avoiding create a new entire view?

Please help me.

Thank you

GrizzlyBear
  • 1,098
  • 1
  • 13
  • 27

4 Answers4

3

If you want the header/footer to be customized in any way other than just a custom string, you will need to create a UIView.

This is documented in the UITableViewDataSource documentation:

tableView:titleForHeaderInSection:

Discussion The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • 1)your link is obsolete, 2)the documentation still contains this text but in reality you can change the header without of writing your custom "headerFooterView" – Vyachaslav Gerchicov Jul 07 '21 at 10:06
2

Sorry but the only way to customize the font color of your header is to create a UIView in the delegate method

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

If you look at the answer posted here then you can see a pretty simple way to implement this into your application. All you have to do is copy and paste that function into your table view's data source and then change the UILabel properties to be whatever you would like it to be.

Here is the code from that post for easy reference:

Originally posted by Harsh:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here u can change the text color of header
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}
Community
  • 1
  • 1
RGullotti
  • 263
  • 2
  • 7
0

Given that if you set the header/footer without a UIView subclass you are passing a NSString, it currently isn't possible to accomplish what you want without having to create a UIView.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
0
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.textColor = ...
}

I am even able to set attributed text to this label

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49