首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更改UITableView节标题的字体大小

更改UITableView节标题的字体大小
EN

Stack Overflow用户
提问于 2013-11-06 09:22:26
回答 3查看 107K关注 0票数 145

有没有人可以告诉我最简单的方法来更改UITableView部分标题中的文本的字体大小?

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

代码语言:javascript
复制
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

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

代码语言:javascript
复制
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

我使用以下方法填充了UITableView单元格:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

然而,我被困在如何实际增加部分标题文本的字体大小或字体样式上?

有没有人能帮忙?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-06 09:47:56

不幸的是,您可能必须覆盖以下内容:

在Objective-C中:

代码语言:javascript
复制
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

在Swift中:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

尝试如下所示:

在Objective-C中:

代码语言:javascript
复制
- (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;
}

在Swift中:

代码语言:javascript
复制
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
}
票数 121
EN

Stack Overflow用户

发布于 2020-09-10 01:37:08

这就是,你必须在这里写几个方法。#Swift 5

代码语言:javascript
复制
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
}

祝你好运

票数 4
EN

Stack Overflow用户

发布于 2020-05-23 13:30:21

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

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

问题是tableView.dequeueReusableHeaderFooterView(withIdentifier:)不像tableView.dequeneReuseCell(:)那样总是返回一个单元格。如果没有人可用,则前者将返回nil。即使它返回一个重用头视图,它也不是原始的类类型,而是一个UITableHeaderFooterView。因此,您需要根据自己的代码做出判断并采取行动。基本上,如果它是nil,就会得到一个全新的标题视图。如果不是nil,强制强制转换,这样你就可以控制了。

代码语言:javascript
复制
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}

    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19802336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档