UITableView titleForSection font
21,189
Solution 1
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.
Solution 2
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;
}
Solution 3
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.
Solution 4
viewForHeaderInSection might work fine... but here's an alternative that works fine for me (Xcode 5 and IOS7 target):
// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];
// Text Color & Alignment
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
[header.textLabel setTextAlignment:NSTextAlignmentCenter];
// Text Font
UIFont *saveFont = header.textLabel.font;
[header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];
// Another way to set the background color
// Note: does not preserve gradient effect of original heade!r
// header.contentView.backgroundColor = [UIColor blackColor];
}
Related videos on Youtube
Author by
camilo
Updated on September 25, 2020Comments
-
camilo over 3 years
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!
-
Jasarien about 14 yearsThis is great, however, UILabel is a direct subclass of UIView, so you don't really need to create a 'header' view and add the label as a subview. You can simply return the label itself as the header view.
-
talkol almost 12 yearsIf you return the label itself as the header, how would you give it some offset on the left? Changing the origin.x of the label frame in that case does not work..
-
Asahi almost 11 yearsto set text of the section title:
label.text = [self tableView:tableView titleForHeaderInSection:section];
-
JScarry almost 11 yearsIf you use Interface Builder to make your table, you'll also have to change the size of the section height in the Attributes Inspector.