Dancer
Dancer

Reputation: 17681

UITableView Header Styling - Font Color / Background / Font Size

Im struggling trying to style a UIView Header, I'm using IOS7 storyboard and custom cells - each of which has its own class -

I would to achieve an opaque grey background with white text - Helvetica Neue Medium Font size 16 - the initial header is hidden in the HeightForHeaderInSection method - So far I have -

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  tableView.bounds.size.width, 30)];
    if (section == 0)
        [headerView setBackgroundColor:[UIColor clearColor]];
    else
        [headerView setBackgroundColor:[UIColor colorWithRed:156.0f/255.0f green:156.0f/255.0f blue:156.0f/255.0f alpha:0.75f]];
     [headerView setTintColor:[UIColor whiteColor]];


    return headerView;

 }

Update My Header text is taken from this method -

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{


    if(section == 1)
    {
        return @"Offers";
    }
    else if(section == 2)
    {
        return @"Workouts";
    }
    else
    {
    return @"Weights";
    }
}

Upvotes: 1

Views: 7651

Answers (1)

Ilario
Ilario

Reputation: 6079

you can give the title of each header section in viewForHeaderInSection in this way:

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

   UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  tableView.bounds.size.width, 30)];

   UILabel *labelHeader = [[UILabel alloc] initWithFrame:CGRectMake (0,0,320,30);
   labelHeader.font = [UIFont ...]
   labelHeader.textColor = [UIColor whiteColor];

   [headerView addSubview:labelHeader];

if (section == 0)
    [headerView setBackgroundColor:[UIColor clearColor]];
else if (section == 1) {

    [headerView setBackgroundColor:[UIColor colorWithRed:156.0f/255.0f green:156.0f/255.0f blue:156.0f/255.0f alpha:0.75f]];
    labelHeader.text = @"Offers"
 }
else if (section == 2) {
    [headerView setBackgroundColor:[UIColor colorWithRed:156.0f/255.0f green:156.0f/255.0f blue:156.0f/255.0f alpha:0.75f]];
    labelHeader.text = @"Workouts"

 }

else {

   [headerView setBackgroundColor:[UIColor colorWithRed:156.0f/255.0f green:156.0f/255.0f blue:156.0f/255.0f alpha:0.75f]];
    labelHeader.text = @"Weights"
 }
}

Upvotes: 3

Related Questions