views:

215

answers:

2

I tried to implement the

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

to get the text label of the header in section of black color instead of the white color, but it looks so different from the default one created by the SDK, mine is so ugly.

How to recreate the UIView with exactly the same as the one from the SDK?

From Apple documentation:

Discussion
The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.

A: 

You can try to mimic it using shadowColor and shadowOffset.

I have done it in black for example using code like the following:

label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(1, 1);

By messing with the colors and shadow combinations, you can mimic the label pretty well.

rickharrison
hi rick, how about the background? which color should I use?
sfa
Are you putting it in a grouped table view? If so you should have a background color of [UIColor clearColor];
rickharrison
No, rick, this is not what I want. From the discussion of the Apple documentation as I quoted in the question, I want to re-create the custom view that is the same as the default section header titles from Apple with the text color in black, instead of the default white color. The problem is that the default background is so special, I don't know which color is that, and it looks like it is gradient.
sfa
So are you talking about the section headers in a grouped or plain table view. That makes a big difference hence me asking if its in a grouped table view.
rickharrison
no, it's the plain table view, not grouped table , sorry I missed this information.
sfa
+1  A: 

Although this is not exactly right, I'm posting this in hopes that someone can improve upon my approach. The text is right (white like default, but you can change that). The background gradient and opacity are not perfect (too light??)--some tweaking here might be needed.

Get the sectionheaderbackground.png here: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

// Create a stretchable image that emulates the default gradient
UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];

// Create the view for the header
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
sectionView.alpha = 0.9;
sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];

// Create the label
CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
sectionLabel.text = @"Test section label";
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.shadowColor = [UIColor grayColor];
sectionLabel.shadowOffset = CGSizeMake(0, 1);
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionView addSubview:sectionLabel];
[sectionLabel release];

// Return the header section view
return sectionView;
dferg
thanks for your answer! I'll try and let you know. The trick here is to use the existing image as the background color :) I thought that they use a color with name :p
sfa