23

I am making an iPhone app where in I have a grouped TableView with headers for the sections.

Problem is that I want to change the Section Header's text color.

How can I change the text color of Section Header?

What should I do?

halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

7 Answers7

54

Add the following code to your AppDelegate class in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
Vahan
  • 2,176
  • 19
  • 17
  • I added it to my the TableViews DataSource but other than that it works great and is a lot less code than the other solutions. – Ben Mar 27 '14 at 01:40
  • 2
    Added it to 'tableView: titleForHeaderInSection:' and it works like a charm! – AlexanderZ Jun 23 '14 at 12:41
  • 5
    For iOS 9+, should use `[[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setTextColor:[UIColor whiteColor]];` instead. – Cai Mar 02 '16 at 02:47
  • @Vahan, Bravo! My regards! – gaussblurinc Apr 15 '17 at 15:46
  • 1
    I've got a problem with this on iOS 11.2.2/Xcode 9.2 where when I scroll the text color reverts back to its original grey color. Seems odd! UIAppearance seems to work everywhere else. Maybe it's a bug. – Mike Meyers Jan 25 '18 at 23:36
  • @MikeMeyers I have the exact same problem. No matter what I do it reverts when I scroll up and down. It's super annoying. – nickdnk Sep 10 '18 at 16:16
39

If you don't want to do it app wide like in Vahan's solution, here is a solution using one of UITableViewDelegate's method :

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
       headerView.textLabel?.textColor = UIColor.redColor() 
    }
}
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
33

This is SURELY gonna work for you.

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

just copy and paste this function in your code.

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Developer
  • 6,375
  • 12
  • 58
  • 92
3

You can implement this table view data source method:

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
   //create your custom label here & anything else you may want to add
   return YourCustomView;
}
Rog
  • 18,602
  • 6
  • 76
  • 97
2

@Harsh 's answer worked great for me, and by changing the coordinations of UILabel you can move it around. Also, I, personally thought to change the shadow offset a bit to make it more readable, but that could be a personal choice. Here's my version in case:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
    //If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor yellowColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.5., 0.5.);
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
    [view addSubview:label];

    return view;
}
Willy
  • 9,681
  • 5
  • 26
  • 25
Septronic
  • 1,156
  • 13
  • 32
  • If you use something like this, make sure to also `implement - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section` – Sofi Software LLC Nov 17 '12 at 03:59
2
 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UILabel *myLabel = [[UILabel alloc] init];
  myLabel.frame = CGRectMake(20, 8, 220, 20);
  myLabel.font = [UIFont boldSystemFontOfSize:16];
  myLabel.text = [self tableView:tableView 
  titleForHeaderInSection:section];
  myLabel.backgroundColor=[UIColor grayColor];
  UIView *headerView = [[UIView alloc] init];
  [headerView addSubview:myLabel];
  return headerView;
  }
Sagar Rathode
  • 243
  • 1
  • 15
2

I built off of the answer from @Harsh.

This is the closest I could get, indistinguishable from what I can tell.

It goes in the <UITableViewDataSource> obviously.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    hView.backgroundColor=[UIColor clearColor];

    UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];

    hLabel.backgroundColor=[UIColor clearColor];
    hLabel.shadowColor = [UIColor whiteColor];
    hLabel.shadowOffset = CGSizeMake(0.5,1);  // closest as far as I could tell
    hLabel.textColor = [UIColor blackColor];  // or whatever you want
    hLabel.font = [UIFont boldSystemFontOfSize:17];
    hLabel.text = @"Your title here";  // probably from array

    [hView addSubview:hLabel];

    return hView;
}
jpswain
  • 14,642
  • 8
  • 58
  • 63