Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView titleForSection font

A quick question, for a quick answer (since I'm not finding any):

Is there a way to change the font of the section's title (given by titleForSection) in iPhone?

Thanks a lot!

like image 997
camilo Avatar asked Mar 15 '10 22:03

camilo


3 Answers

Thanks Jasarien! You were absolutely right.

I leave my code here to help someone with the same problem:

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

   NSString *sectionTitle = @"Just a title";

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 0, 284, 23);
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"Helvetica" size:14];
    label.text = sectionTitle;
    label.backgroundColor = [UIColor clearColor];

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

    return view;
}
like image 133
camilo Avatar answered Oct 24 '22 05:10

camilo


You'll have to use the viewForHeaderInSection: method and provide your own view. Luckily this can be a UILabel with a specified font, so you can do it quite easily.

like image 33
Jasarien Avatar answered Oct 24 '22 05:10

Jasarien


You have to override

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

to give required height for the section title. Else it will overlap with the cell.

like image 5
ArunGJ Avatar answered Oct 24 '22 06:10

ArunGJ