【问题标题】:How to change font color of the title in grouped type UITableView?如何更改分组类型 UITableView 中标题的字体颜色?
【发布时间】:2011-10-29 15:55:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios iphone uitableview fonts


    【解决方案1】:

    如果您只需要更改标题的颜色或字体,请使用tableView: willDisplayHeaderView: forSection:。以下是 swift 中的示例:

    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
        }
    }
    

    原文:

    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 的东西 :)
    【解决方案2】:

    斯威夫特 4.2

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

    【讨论】:

    • 我可以用它来改变页脚吗?
    • 不幸的是,这会改变页眉和页脚。
    【解决方案3】:

    是的...现在效果很好!

    我创建了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 也可以。
    【解决方案4】:
    if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
        [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
    }
    

    【讨论】:

      【解决方案5】:

      使用默认坐标和 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;
      }
      

      【讨论】:

      • @TonyK。你能告诉我在哪里吗?
      • 我的评论在 ARC 中不再相关,但对于旧版本,视图必须自动发布。
      【解决方案6】:

      From apple documentation

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

      所以使用下面的方法并返回您的自定义视图 (UILabel) 以及您选择的字体。

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

      Read from apple documentation

      【讨论】:

        猜你喜欢
        • 2010-10-23
        • 2011-03-18
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多