How to change font color of the title in grouped type UITableView?

28,870

Solution 1

Yes... It works great now!

I created tableView:viewForHeaderInSection: method and created a UIView

UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];

Then i created a UILabel & set the text values & colors to the label. Then i added the label to the view

UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
titleLabel.text = @"<Title string here>";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[customTitleView addSubview:titleLabel];

So my tableView:viewForHeaderInSection: method looks like...

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

    UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
    UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    titleLabel.text = @"<Title string here>";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [customTitleView addSubview:titleLabel];
    return customTitleView;
}

We should add tableView:heightForHeaderInSection: method for providing some space to the title.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    return 44; 
}

Solution 2

If you just need to change the color or font on the header, use tableView: willDisplayHeaderView: forSection:. Here is an example in swift:

Swift v5:

override public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = UIColor.blue
        view.textLabel?.backgroundColor = UIColor.clear
        view.textLabel?.textColor = UIColor.white
    }
}

Original:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = ThemeBlue
        view.textLabel.backgroundColor = UIColor.clearColor()
        view.textLabel.textColor = UIColor.whiteColor()
    }

}

Solution 3

To use the default coordinates and the sections in TableView, with white-color font and shadow:

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

    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 8, 320, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor grayColor];
    label.shadowOffset = CGSizeMake(-1.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    UIView *view = [[UIView alloc] init];
    [view addSubview:label];

    return view;
}

Solution 4

if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}

Solution 5

From apple documentation

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.

So use the below method and return your custom view (UILabel) with your choice of font.

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

Read from apple documentation

Share:
28,870

Related videos on Youtube

Confused
Author by

Confused

Updated on July 09, 2022

Comments

  • Confused
    Confused almost 2 years

    I have a grouped type table view and it looks pretty cool.

    But, if I change the background color of the table to black, the titles becomes unclear.

    Is it possible to change the font color and its styles so that I can make it more readable? Should I implement the tableView:viewForHeaderInSection: method?

  • KlimczakM
    KlimczakM almost 10 years
    @TonyK. Can you please tell where?
  • Tony K.
    Tony K. almost 10 years
    My comment is no longer relevant in ARC, but with older versions the view must be autoreleased.
  • Bjorn Thor Jonsson
    Bjorn Thor Jonsson over 8 years
    As I'm still stubbornly using Objective-C, here's how it looks in that language: - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if( [view isKindOfClass:[UITableViewHeaderFooterView class]] ) { ... ((UITableViewHeaderFooterView *)view).textLabel.textColor = [UIColor redColor]; } ...guess I should give that Swift thing a try :)
  • GeneCode
    GeneCode about 7 years
    Actually you don't need to make that UIView. Just directly return the UILabel as the headerView is fine too.
  • Kurt Lane
    Kurt Lane about 3 years
    Can I use this to change just the footer?
  • Zoltan Vinkler
    Zoltan Vinkler about 3 years
    Unfortunately, this changes both the header and footer.