UITableView titleForHeaderInSection shows all caps

前端 未结 14 924
孤独总比滥情好
孤独总比滥情好 2021-01-30 16:28

I am using titleForHeaderInSection to show a header for a UITableView section. It worked fine with the iOS6 SDK, but the iOS7 SDK shows the header in all CAPS.

I guess

14条回答
  •  鱼传尺愫
    2021-01-30 17:10

    One solution I found is to utilize UITableViewHeaderFooterView.

    Instead of

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return @"some title";
    }
    

    Do

    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *identifier = @"defaultHeader";
        UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
        if (!headerView) {
            headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];
        }
        headerView.textLabel.text = @"some title";
        return headerView;
    }
    

    The annoying downside is that the table view will no longer automatically adjust the section header height for you. So if your header heights varies, you'll have to do something like this:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        id headerAppearance = [UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil];
        UIFont *font = [headerAppearance font];
        CGFloat viewWidth = CGRectGetWidth(tableView.frame);
        CGFloat xInset = 10;
        CGFloat yInset = 5;
        CGFloat labelWidth = viewWidth - xInset * 2;
        CGSize size = [sectionInfo.name sizeWithFont:font constrainedToSize:CGSizeMake(labelWidth, MAXFLOAT)];
        return size.height + yInset * 2;
    }
    

    I really don't like hard-coding layout information (the inset) this way, as it might break in the future version. If anyone has a better solution to get/set the header height, I'm all ears.

提交回复
热议问题