我们如何改变tableview header的字体?

4

我正在使用一些背景颜色来为tableView设置样式,而样式是分组的。但是在各个部分的header中的文字不够清晰,因此我需要修改文字颜色,使得header的文字更加可见。 我想知道是否可以更改header文字的颜色和大小?

5个回答

9

继terente的回答之后,我想补充一些内容:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
        //headerView.contentMode = UIViewContentModeScaleToFill;

        // Add the label
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -5.0, 300.0, 90.0)];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.opaque = NO;
        headerLabel.text = @"Header";
        headerLabel.textColor = [UIColor blackColor];
        headerLabel.highlightedTextColor = [UIColor blackColor];

        //this is what you asked
        headerLabel.font = [UIFont boldSystemFontOfSize:17];

        headerLabel.shadowColor = [UIColor clearColor];
        headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
        headerLabel.numberOfLines = 0;
        headerLabel.textAlignment = UITextAlignmentCenter;
        [headerView addSubview: headerLabel];

        [headerLabel release];  

        // Return the headerView
        return headerView;
    }
    else return nil;
}

您可以使用[UIFont fontWithName:@"<字体名称>" size:24.0];来使用其他字体。


感谢xs2bush的快速回复,现在我能够更改标题颜色了。 - User97693321
headerLabel.textColor = [UIColor blackColor];更改为您想要的颜色。 - Bushra Shahid
1
不要忘记实现 tableView:heightForHeaderInSection:,否则你的分区头将无法适应你的新视图的正确高度。 - Andrew
还要记得将视图缓存到某个地方,否则每次部分标题出现时都会重新绘制视图,这可能会很耗费资源。 - Daniel Wood

5

只需实现

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

并返回您自定义的标题视图。

编辑:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImageView *headerTitleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kSectionHeaderHeight)];
    [headerTitleView setImage:sectionHeaderBackgroundImage];

    UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 38) / 2, 5, 38, kSectionHeaderHeight - 10)];
    sectionTitleLabel.textColor = [UIColor redColor];
    sectionTitleLabel.backgroundColor = [UIColor clearColor];
    sectionTitleLabel.textAlignment = UITextAlignmentCenter;
    sectionTitleLabel.text = @"A";
    sectionTitleLabel.font = [UIFont fontWithName:@"yourFont" size:13];
    [sectionTitleLabel setAdjustsFontSizeToFitWidth:YES];
    [headerTitleView addSubview:sectionTitleLabel];

    return headerTitleView;
}

0
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
    [headerView.textLabel setFont:[UIFont fontWithName:@"Gotham Book" size:16.0f]];
}}

我们可以使用 header.textlabel 对象来更改该标签的其他“UILabel”属性。

0
- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{

    [((UITableViewHeaderFooterView) *view).textLabel setFont:(UIFont...)];
}

你可以从其他表视图委托方法中设置文本标签。


0
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    [((UITableViewHeaderFooterView *) view).textLabel setFont:[UIFont fontWithName:@"Your-Font-Name" size:((UITableViewHeaderFooterView *) view).textLabel.font.pointSize]];
}


注:

  • 这将会设置自定义字体,但保持pointSize不变。
  • willDisplayFooterView也适用。
  • 别忘了将Your-Font-Name更改为您的字体名称。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接