147

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

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

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

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

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

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

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

但是,我对如何实际增加节标题文本的字体大小 - 或者就此而言字体样式 - 感到困惑?

有人可以帮忙吗?谢谢。

4

11 回答 11

389

另一种方法是响应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;
}

斯威夫特 1.2

(注意:如果您的视图控制器是 a 的后代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
}

斯威夫特 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
}
于 2014-04-11T19:55:03.887 回答
122

不幸的是,您可能必须覆盖它:

在 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
}
于 2013-11-06T01:47:55.693 回答
97

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

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

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

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

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

于 2014-01-12T03:03:38.850 回答
27

对于 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          
    }
}
于 2014-09-29T20:00:02.080 回答
20

斯威夫特 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)
    }
}
于 2016-09-22T16:11:34.483 回答
8

斯威夫特 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!
  }
于 2015-12-27T15:11:49.647 回答
4

在这里,你必须遵循在这里写一些方法。#斯威夫特 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
}

祝你好运

于 2020-09-09T17:37:07.563 回答
4

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

第一种方法

- (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;
}
于 2016-12-15T10:56:11.583 回答
4

Swift 4版本的 Leo Natan答案

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
}
于 2018-01-11T14:04:58.537 回答
1

斯威夫特 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)
        }
    }
于 2016-07-07T20:53:44.030 回答
-1

这是我使用 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}

    }
于 2020-05-23T05:30:21.347 回答