Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mimic UITableView's UITableViewStylePlain section header style

My application uses abbreviations in UITableView section header titles that are hard for VoiceOver to pronounce. As I need to make these titles pronounceable by VoiceOver, I need to give the section header title a accessibilityLabel.

It seems that the only way to do this is to draw a custom section header cell. I would like to mimic the standard Apple UIKit provided style for these custom section headers but I am uncertain on how to emulate Apple's detailed look of this element.

What is the best approach to mimic the UITableViewStylePlain section header style?

Update: I am well aware how to create a custom header cell. What I am looking for is a technique to mimic exactly the look of the header cell style as provided by Apple for the plain UITableView section header cells.

like image 966
Bringo Avatar asked Jan 12 '11 01:01

Bringo


1 Answers

If anyone is still interested, I've got it looking pretty damn close with the following code (using Mark Adams images from the comment above, but I resized them slightly as my app also has landscape mode):

- (UIView *)tableView:(UITableView *)tbl viewForHeaderInSection:(NSInteger)section
{
    UIView* sectionHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.bounds.size.width, 18)];
    sectionHead.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    sectionHead.userInteractionEnabled = YES;
    sectionHead.tag = section;

    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PlainTableViewSectionHeader.png"]];
    headerImage.contentMode = UIViewContentModeScaleAspectFit;

    [sectionHead addSubview:headerImage];
    [headerImage release];

    UILabel *sectionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, tbl.bounds.size.width - 10, 18)];
    sectionText.text = text;
    sectionText.backgroundColor = [UIColor clearColor];
    sectionText.textColor = [UIColor whiteColor];
    sectionText.shadowColor = [UIColor darkGrayColor];
    sectionText.shadowOffset = CGSizeMake(0,1);
    sectionText.font = [UIFont boldSystemFontOfSize:18];

    [sectionHead addSubview:sectionText];
    [sectionText release];

    return [sectionHead autorelease];
}
like image 176
BarrettJ Avatar answered Sep 28 '22 16:09

BarrettJ