简体   繁体   中英

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

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.

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

假设如果您在没有UIView子类的情况下设置页眉/页脚,则您将传递NSString ,那么当前无需创建UIView就无法完成所需的操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM