Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 421
Confused Avatar asked Aug 18 '11 10:08

Confused


4 Answers

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()
    }

}
like image 138
Code Commander Avatar answered Nov 20 '22 09:11

Code Commander


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;
}
like image 21
lucasart Avatar answered Nov 20 '22 08:11

lucasart


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; 
}
like image 21
Confused Avatar answered Nov 20 '22 10:11

Confused


if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}
like image 2
Nitesh Borad Avatar answered Nov 20 '22 08:11

Nitesh Borad