如何在分组样式的UITableView中更改标题的字体颜色?

45

我有一个分组类型的表视图,看起来很酷。

但是,如果我将表的背景颜色更改为黑色,标题就变得不清晰了。

是否可以更改字体颜色及其样式,使其更易读?我应该实现 tableView:viewForHeaderInSection: 方法吗?

6个回答

55
如果你只是想改变表头的颜色或字体,可以使用tableView: willDisplayHeaderView: forSection:。以下是Swift v5的示例代码:
override public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = UIColor.blue
        view.textLabel?.backgroundColor = UIColor.clear
        view.textLabel?.textColor = UIColor.white
    }
}

Original:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = ThemeBlue
        view.textLabel.backgroundColor = UIColor.clearColor()
        view.textLabel.textColor = UIColor.whiteColor()
    }

}

由于我仍然固执地使用Objective-C,以下是它在该语言中的样子:
  • (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if( [view isKindOfClass:[UITableViewHeaderFooterView class]] ) { ... ((UITableViewHeaderFooterView *)view).textLabel.textColor = [UIColor redColor]; }
...猜想我应该尝试一下那个Swift :)
- Bjorn Thor Jonsson

43

如果想要使用默认坐标和TableView中的章节,并设置白色字体和阴影:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 8, 320, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor grayColor];
    label.shadowOffset = CGSizeMake(-1.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    UIView *view = [[UIView alloc] init];
    [view addSubview:label];

    return view;
}

1
@TonyK. 你能告诉我在哪里吗? - KlimczakM
1
我的评论在ARC中已经不再相关,但是在旧版本中,视图必须被自动释放。 - Tony K.

21

好的...现在它工作得非常好!

我创建了tableView:viewForHeaderInSection:方法并创建了一个UIView。

UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];

我创建了一个UILabel,设置了标签的文本值和颜色。然后将标签添加到视图中。

UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
titleLabel.text = @"<Title string here>";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[customTitleView addSubview:titleLabel];

我的tableView:viewForHeaderInSection:方法看起来像...

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

    UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
    UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    titleLabel.text = @"<Title string here>";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [customTitleView addSubview:titleLabel];
    return customTitleView;
}

我们应该添加tableView:heightForHeaderInSection:方法来为标题提供一些空间。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    return 44; 
}

其实你不需要创建那个UIView,直接返回UILabel作为headerView也可以。 - GeneCode

2
if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}

1

来自苹果文档

表视图使用固定的字体样式作为节标题。如果您想要不同的字体样式,请在委托方法tableView:viewForHeaderInSection:中返回自定义视图(例如,一个UILabel对象)。

因此,请使用以下方法,并返回您选择的字体的自定义视图(UILabel)。

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

从苹果文档中阅读


0

Swift 4.2

UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white

我可以使用这个来仅更改页脚吗? - Kurt L.
很遗憾,这会同时改变页眉和页脚。 - Zoltan Vinkler

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