【问题标题】:Changing Font Size For UITableView Section Headers更改 UITableView 部分标题的字体大小
【发布时间】:2013-11-17 02:45:21
【问题描述】:

有人可以指导我更改 UITableView 部分标题中文本的字体大小的最简单方法吗?

我使用以下方法实现了部分标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

然后,我了解了如何使用此方法成功更改节标题高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

我使用这种方法填充了 UITableView 单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

但是,我不知道如何实际增加节标题文本的字体大小 - 或者就此而言是字体样式?

有人可以帮忙吗?谢谢。

【问题讨论】:

标签: ios objective-c swift uitableview uitableviewsectionheader


【解决方案1】:

另一种方法是响应UITableViewDelegate 方法willDisplayHeaderView。传递的视图实际上是UITableViewHeaderFooterView 的一个实例。

下面的示例更改了字体,并且还使标题文本在单元格内垂直和水平居中。请注意,您还应该回复heightForHeaderInSection,以便在表格视图的布局中考虑对标题高度的任何更改。 (也就是说,如果您决定在此willDisplayHeaderView 方法中更改标题高度。)

然后您可以响应titleForHeaderInSection 方法,以使用不同的节标题重用这个配置的标题。

Objective-C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;

    header.textLabel.textColor = [UIColor redColor];
    header.textLabel.font = [UIFont boldSystemFontOfSize:18];
    CGRect headerFrame = header.frame;
    header.textLabel.frame = headerFrame;
    header.textLabel.textAlignment = NSTextAlignmentCenter;
}

Swift 1.2

(注意:如果您的视图控制器是UITableViewController 的后代,则需要将其声明为override func。)

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{
    let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView

    header.textLabel.textColor = UIColor.redColor()
    header.textLabel.font = UIFont.boldSystemFontOfSize(18)
    header.textLabel.frame = header.frame
    header.textLabel.textAlignment = NSTextAlignment.Center
}

Swift 3.0

如果您的标题视图不是 UITableViewHeaderFooterView,此代码还可以确保应用不会崩溃:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let header = view as? UITableViewHeaderFooterView else { return }
    header.textLabel?.textColor = UIColor.red
    header.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    header.textLabel?.frame = header.bounds
    header.textLabel?.textAlignment = .center
}

【讨论】:

  • 这种方法对我来说比上面的方法好得多
  • 我见过的最佳答案。
  • 这将是调整信息的“正确”方式,假设没有其他原因进行子类化(例如添加视图)。此外,此方法可用于更新动态类型的标题文本。只需使用:header.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 和/或 header.detailTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 以及所需的其他步骤(请参阅此处:captechconsulting.com/blog/john-szumski/…
  • 这不会调整标题视图的大小,因此如果您的字体较大或显着不同,例如 Zapfino(不要问为什么),它会将文本剪切掉。如果你必须自己计算大小,那就是一团糟,你不应该这样做。
  • @CocoaPriest 在我的测试版中不会崩溃,尽管如此。 (转基因种子 2)
【解决方案2】:

很遗憾,您可能必须重写:

在 Objective-C 中:

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

在斯威夫特中:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

试试这样的:

在 Objective-C 中:

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

    UILabel *myLabel = [[UILabel alloc] init];
    myLabel.frame = CGRectMake(20, 8, 320, 20);
    myLabel.font = [UIFont boldSystemFontOfSize:18];
    myLabel.text = [self tableView:tableView titleForHeaderInSection:section];

    UIView *headerView = [[UIView alloc] init];
    [headerView addSubview:myLabel];

    return headerView;
}

在斯威夫特中:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let myLabel = UILabel()
    myLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 20)
    myLabel.font = UIFont.boldSystemFont(ofSize: 18)
    myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)

    let headerView = UIView()
    headerView.addSubview(myLabel)

    return headerView
}

【讨论】:

  • 谢谢。这非常有效。非常感谢。
  • 虽然这是一个正确的解决方案,但要小心这种方法。对于超过一行的标题,您必须在tableView:heightForHeaderInSection: 中执行标题高度的计算,这可能很麻烦。
  • 试过了,如果向上滚动表格,它会起作用,但标题标签会留在屏幕上并覆盖单元格。 :(
  • @trss 我想你会发现这不是预期的行为。我不是在谈论留在那里的标题部分,只有标签,以及当它们从它下面经过时它对单元格的超级强加,使它看起来一团糟。我确实找到了一种更好的方法来实现这一点,一旦我再次找到它就会把它发回来。
  • @mosca1337 不需要创建另一个视图,您可以获取实际显示的'UITableViewHeaderFooterView'并调整参数。
【解决方案3】:

虽然 mosca1337 的答案是正确的解决方案,但请小心使用该方法。对于文本超过一行的标题,您必须在tableView:heightForHeaderInSection: 中执行标题高度的计算,这可能很麻烦。

一种更受欢迎的方法是使用外观 API:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont boldSystemFontOfSize:28]];

这将更改字体,同时仍让表格自行管理高度。

为获得最佳结果,将表视图子类化,并将其添加到包含链(appearanceWhenContainedIn:)中,以确保仅针对特定表视图更改字体。

【讨论】:

  • 如果子类化,那么您无论如何都会从- tableView:viewForHeaderInSection: 返回一个自定义视图,对吗?在这种情况下,字体可以在那里设置。这就是 @mosca1337 的解决方案所做的。
  • 哈哈,昨天之后我就疯了。子类化表视图并将其添加到容器列表中。 ;-)
  • 此解决方案会导致计算实际页脚/页眉大小时出现许多错误。我可以展示一些在设置自定义字体时标题被剪切的示例。
  • Swift 3UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
  • 这不会正确调整标签的大小以适应 iOS 11 上的字体。此外,加载视图后上下滚动会将它们重置为默认字体。
【解决方案4】:

对于 iOS 7 我使用这个,


-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;

    header.textLabel.font = [UIFont boldSystemFontOfSize:10.0f];
    header.textLabel.textColor = [UIColor orangeColor];
}

这里是 Swift 3.0 版本,带有标题大小调整

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel!.font = UIFont.systemFont(ofSize: 24.0)
        header.textLabel!.textColor = UIColor.orange          
    }
}

【讨论】:

  • 这不会调整标题视图以适应新字体。
  • @LeoNatan 我们如何调整标题视图以适应新字体 - 可以用这种方法完成吗?
  • 我想澄清一下,我确实看到了您的上述答案,但我只想在用户选择的字体(可访问性)超过一定大小时更改字体以限制大小(因此,并非所有时间)。我相信我需要检查并可能更改 willDisplayHeaderView 中的字体,所以如果字体更改,有没有办法重新计算视图高度?
【解决方案5】:

斯威夫特 3:

调整尺寸的最简单方法:

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

    let header = view as! UITableViewHeaderFooterView

    if let textlabel = header.textLabel {
        textlabel.font = textlabel.font.withSize(15)
    }
}

【讨论】:

  • 这是我正在寻找的最简单的方法。
  • 在 swift 4 中工作!不要忘记“覆盖函数..”
【解决方案6】:

Swift 2.0

  1. 用完全可定制的 UILabel 替换默认节标题。

实现viewForHeaderInSection,像这样:

  override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
    if sectionTitle == "" {
      return nil
    }

    let title: UILabel = UILabel()

    title.text = sectionTitle
    title.textColor = UIColor(red: 0.0, green: 0.54, blue: 0.0, alpha: 0.8)
    title.backgroundColor = UIColor.clearColor()
    title.font = UIFont.boldSystemFontOfSize(15)

    return title
  }
  1. 更改默认标题(保持默认)。

实现willDisplayHeaderView,像这样:

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

    if let view = view as? UITableViewHeaderFooterView {
      view.backgroundView?.backgroundColor = UIColor.blueColor()
      view.textLabel!.backgroundColor = UIColor.clearColor()
      view.textLabel!.textColor = UIColor.whiteColor()
      view.textLabel!.font = UIFont.boldSystemFontOfSize(15)
    }
  }

记住:如果你使用的是静态单元格,由于 UITableView 的顶部,第一个节标题比其他节标题填充得更高;解决这个问题:

实现 heightForHeaderInSection,如下所示:

  override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 30.0 // Or whatever height you want!
  }

【讨论】:

    【解决方案7】:

    Swift 4 版本的 Leo Natan answer

    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
    

    如果你想设置自定义字体,你可以使用

    if let font = UIFont(name: "font-name", size: 12) {
        UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = font
    }
    

    【讨论】:

    • 不幸的是,这不会调整框架的大小。
    【解决方案8】:

    就是这样,你得跟着在这里写几个方法。 #斯威夫特 5

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        
        let header = view as? UITableViewHeaderFooterView
        header?.textLabel?.font = UIFont.init(name: "Montserrat-Regular", size: 14)
        header?.textLabel?.textColor = .greyishBrown
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 26
    }
    

    祝你好运

    【讨论】:

      【解决方案9】:

      使用此方法,您还可以设置字体大小、字体样式和页眉背景。 这个有两种方法

      第一种方法

      - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
              UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
              header.backgroundView.backgroundColor = [UIColor darkGrayColor];
              header.textLabel.font=[UIFont fontWithName:@"Open Sans-Regular" size:12];
              [header.textLabel setTextColor:[UIColor whiteColor]];
          }
      

      第二种方法

      - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
      //    myLabel.frame = CGRectMake(20, 8, 320, 20);
          myLabel.font = [UIFont fontWithName:@"Open Sans-Regular" size:12];
          myLabel.text = [NSString stringWithFormat:@"   %@",[self tableView:FilterSearchTable titleForHeaderInSection:section]];
      
          myLabel.backgroundColor=[UIColor blueColor];
          myLabel.textColor=[UIColor whiteColor];
          UIView *headerView = [[UIView alloc] init];
          [headerView addSubview:myLabel];
          return headerView;
      }
      

      【讨论】:

        【解决方案10】:

        斯威夫特 2:

        按照 OP 的要求,调整大小,而不是将其设置为系统粗体字体或其他:

        func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                if let headerView = view as? UITableViewHeaderFooterView, textLabel = headerView.textLabel {
        
                    let newSize = CGFloat(16)
                    let fontName = textLabel.font.fontName
                    textLabel.font = UIFont(name: fontName, size: newSize)
                }
            }
        

        【讨论】:

          【解决方案11】:

          这是我使用 swift 5 的解决方案。

          要完全控制标题部分视图,您需要在控制器中使用 tableView(:viewForHeaderInsection::) 方法,如上一篇文章所示。但是,还有更进一步的步骤:为了提高性能,苹果建议不要每次都生成新视图,而是重用标题视图,就像重用表格单元格一样。这是通过方法 tableView.dequeueReusableHeaderFooterView(withIdentifier: )。但是我遇到的问题是,一旦您开始使用此重用功能,字体将无法按预期运行。其他的东西,比如颜色,对齐都很好,但只是字体。有一些讨论,但我让它像下面这样工作。

          问题是 tableView.dequeueReusableHeaderFooterView(withIdentifier:) 不像 tableView.dequeneReuseCell(:) 总是返回一个单元格。如果没有可用的,前者将返回 nil。即使它返回一个重用的标题视图,它也不是你原来的类类型,而是一个 UITableHeaderFooterView。所以你需要在自己的代码中做判断和行动。基本上,如果它是 nil,则获得一个全新的标题视图。如果不为零,则强制施放,以便您可以控制。

          override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                  let reuse_header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "yourHeaderID")
                  if (reuse_header == nil) {
                      let new_sec_header = YourTableHeaderViewClass(reuseIdentifier:"yourHeaderID")
                      new_section_header.label.text="yourHeaderString"
                      //do whatever to set color. alignment, etc to the label view property
                      //note: the label property here should be your custom label view. Not the build-in labelView. This way you have total control.
                      return new_section_header
                  }
                  else {
                      let new_section_header = reuse_section_header as! yourTableHeaderViewClass
                      new_sec_header.label.text="yourHeaderString"
                      //do whatever color, alignment, etc to the label property
                      return new_sec_header}
          
              }
          

          【讨论】:

            猜你喜欢
            • 2011-12-18
            • 2022-06-24
            • 2012-10-07
            • 1970-01-01
            • 2019-02-05
            • 1970-01-01
            • 2019-01-30
            • 2010-10-23
            • 2011-03-18
            相关资源
            最近更新 更多