jmr1706
jmr1706

Reputation: 249

Is there a way to change the font color of all instances of UITableview's header

The new iOS 7 has changed the default font color of the section headers in tableview. The problem is that my background image makes the text hard to read. I know I could change my background but I would like to change the color of all the textviews. I have changed the uinavigationbar color in my apps delegate before. I would like to use something like that for tableview if possible. I have read this method:

NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];

if (sectionTitle == nil) {
    return nil;
}else{
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 8, 320, 16);
    label.textColor = [UIColor whiteColor];
    label.text = sectionTitle;

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

    return view;
}

My problem with this is that I would have to implement it on a per tableviewcontroller basis. Also when using this I'm not sure how to prevent the text from going off the page and being unreadable.

Any suggestions would be great. Thanks in advance.

EDIT: I have decided to add a little clarification just for show using some suggested code here remains my problem with this solution. UITableView Header trails off

EDIT: To accompany answer. I found that this is also needed to create the space for multiple lines for header.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   if (section == ?) {
       return 60;
   }
   else{
       return 44;
   }
}

Upvotes: 1

Views: 375

Answers (1)

Uniruddh
Uniruddh

Reputation: 4436

You must use following method to change your headerview :

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

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,yourWidth,YourHeight)] ;
headerView.backgroundColor = [UIColor colorWithRed:0.5058f green:0.6118f blue:0.8078f alpha:1.0f];


tableView.sectionHeaderHeight = headerView.frame.size.height;
tableView.tableHeaderView.clipsToBounds = YES;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13,320, 22)] ;
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
    // Chnage your title color here
label.textColor = [UIColor whiteColor];
    [label sizeToFit];
    label.numberOfLines = 2 ;

[headerView addSubview:label];
return headerView;
} 

Upvotes: 2

Related Questions