GrizzlyBear
GrizzlyBear

Reputation: 1199

Change TableView Header Text Color

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

Upvotes: 1

Views: 3902

Answers (4)

Vyachaslav Gerchicov
Vyachaslav Gerchicov

Reputation: 2457

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

Upvotes: 0

RGullotti
RGullotti

Reputation: 263

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;
}

Upvotes: 2

Dima
Dima

Reputation: 23624

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.

Upvotes: 3

Oscar Gomez
Oscar Gomez

Reputation: 18488

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.

Upvotes: 0

Related Questions