Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

beachdog

macrumors member
Original poster
Aug 10, 2008
86
0
I have a table and simply want the header to be drawn in the same font that is used when I look at it in Interface Builder, when using the grouped table style (e.g. "California" in the attached screen shot).

My understanding is that even to just have a simple text title for a heading I need to implement

Code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and create a UILabel -- is that correct? Seems like a lot of work when you just want to supply simple text for the header, but oh well.

So I have done that, but my question is what specific font is used in Interface Builder that I want to use, and also what text color is that? My current code looks like this, but does not look quite the same:

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

UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 40)] autorelease];
hdr.textAlignment = UITextAlignmentCenter;
hdr.font = [UIFont systemFontOfSize:16.0];
hdr.opaque = YES ;
hdr.textColor = [UIColor darkGrayColor];
hdr.text = NSLocalizedString(@"my-header-title",@"");
hdr.backgroundColor = [UIColor clearColor];
return hdr ;
}
 

Attachments

  • table-header.jpg
    table-header.jpg
    15.5 KB · Views: 7,179
If you implement

Code:
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
then you just have to return the string and the table will render it the way you see it in IB. You can also return nil for no header. You should realize that there are headers for sections and a single header for the table that can be set independently.
 
I too would like the answer to this question about creating the font programmatically for use in your own cells
 
Example viewForHeaderInSection

I don't have the exact colors for the text and shadow that you are looking for, but the basic code to do what you are looking for is as follows:

Code:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {   // custom view for header. will be adjusted to default or specified header height
	UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 24.0f)];
	headerView.backgroundColor = [UIColor clearColor];

	UILabel *headerTitle = [[[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 24.0f)] autorelease];
	[headerTitle setBackgroundColor:[UIColor clearColor]];
	[headerTitle setFont:[UIFont boldSystemFontOfSize:14.0f]];
	[headerTitle setTextColor:[UIColor darkGrayColor]];
	[headerTitle setShadowColor:[UIColor shadowColor]];
	[headerTitle setShadowOffset:CGSizeMake(0.0f, 1.0f)];
	
	switch (section) {
		case 0:
			[headerTitle setText:@"Heading 1"];
			break;
		case 1:
			[headerTitle setText:@"Heading 2"];
			break;
		default:
			[headerTitle setText:@""];
			break;
	}
	
	[headerView addSubview:headerTitle];
	return headerView;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.