在Swift iOS 8中更改表格视图(section)标题的字体

34

我想要在表格视图控制器中更改某个部分标题的字体类型和字体大小。

我的代码:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel.textColor = UIColor.blackColor()
    header.textLabel.font = UIFont(name: "Futura", size: 38)!
}

但是这样做行不通。有什么想法吗?


未来字体文件必须在您的项目文件夹中。 - iRiziya
这个字体已经包含在内了。单元格文本也使用了这种字体。 - Trombone0904
好的,那么请在 viewForHeaderInSection 方法中尝试上述代码。并为标题文本取一个标签 var title = UILabel() title.font = UIFont(name: "Futura", size: 38)! title.textColor = UIColor.lightGrayColor() 然后将其添加到头部视图中 header.addSubview(title) - iRiziya
一切正常,上述代码正确无误 - 我把代码放到了错误的Swift文件中 -.- - Trombone0904
11个回答

91
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "Futura", size: 38)!
    header.textLabel?.textColor = UIColor.lightGrayColor()
}

7
这个解决方案似乎比已接受的答案更清晰,并且在Swift 2.0中对我非常有效。 - Clifton Labrum
一个更加简洁的版本是,如果UITableViewHeaderFooterView有一个函数调用inject(font: UIFont, textColor: UIColor),并且视图没有被强制解包。 - T. Hyldgaard

38
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "Futura", size: 11)
    header.textLabel?.textColor = UIColor.lightGrayColor()
}

7
UITableViewHeaderFooterView.textLabel 是只读属性。因此 header.textlabel = title 无法使用。 - Atticus

34

Swift 3

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headerView = UIView()
            headerView.backgroundColor = UIColor.lightGray

            let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
                tableView.bounds.size.width, height: tableView.bounds.size.height))
            headerLabel.font = UIFont(name: "Verdana", size: 20)
            headerLabel.textColor = UIColor.white
            headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
            headerLabel.sizeToFit()
            headerView.addSubview(headerLabel)

            return headerView
        }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

这不起作用 - 你能解释一下headerlabel文本输入吗? - UKDataGeek
它将标签的文本设置为表视图的部分标题。我在运行Swift 3的实时应用程序中使用它。您事先设置了标题吗? - AdamVanBuskirk
你需要确保单元格有一个高度,出于某种原因 - 这使它起作用了。 - UKDataGeek
1
很好的观点,我也设置了高度。我已经将其添加到我的答案中,以供未来的访问者参考。 - AdamVanBuskirk
问题在于你在这一行将标签的高度设置为表格的高度: let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height)) - Claudio Castro

14

已更新至Swift 3

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

    let header = view as? UITableViewHeaderFooterView
    header?.textLabel?.font = UIFont(name: "Futura", size: 12) // change it according to ur requirement
    header?.textLabel?.textColor = UIColor.red // change it according to ur requirement
}

7
我发现最简单的方法是在您的视图控制器或表视图控制器中执行以下操作。
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "FontName", size: 14)


}

6

简单的工作解决方案:(Swift 2.0)

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

            //Create label and autoresize it
            let headerLabel = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 2000))
            headerLabel.font = UIFont(name: "Avenir-Light", size: 30)
            headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
            headerLabel.sizeToFit()

            //Adding Label to existing headerView
            let headerView = UIView()
            headerView.addSubview(headerLabel)

            return headerView
}

2
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, 
forSection section: Int) {
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.font = UIFont(name: "Futura", size: 14)
}

请参考上面@Atticus的答案! - Fattie

2
 func tableView(tableView: UITableView,
        viewForHeaderInSection section: Int) -> UIView? {
                let hView = UIView(frame: CGRectMake(0, 0, tableView.frame.width, 44))
                hView.backgroundColor = UIColor.whiteColor()
                let hLabel = UILabel(frame: CGRectMake(15, 2, 30, 44))
                hLabel.font = UIFont(name: "YOUR_FONT_NAME", size: 30)
                hLabel.textColor = kiExtremeOrange
                hLabel.text = alphabets[section]
                hView.addSubview(hLabel)
                return hView
}

注:首先导入您想要使用的字体。

1

Xamarin/C#

基于Atticus的答案,使用willDisplayHeaderView

public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
    var header = headerView as UITableViewHeaderFooterView;
    header.TextLabel.Font = UIFont.FromName("Futura", header.TextLabel.Font.PointSize);
}

请注意,Xamarin是一个跨平台的开发工具,用于使用C#语言构建iOS应用程序。上面的代码是C#代码,在Xcode中无法运行。这个答案只针对那些使用Xamarin但仍想实现OP所需的开发者。

0

已更新至 Xcode 11,Swift 5

对我来说,得票最高的答案都不起作用,这是我成功的方法

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

    /// Create the view.
    let headerView = UIView()
    headerView.backgroundColor = .clear

    /// Create the label that goes inside the view.
    let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView.bounds.size.width, height: 30))
    headerLabel.font = UIFont(name: "myFont", size: 12)
    headerLabel.textColor = .white
    headerLabel.text = "myHeaderName"
    headerLabel.sizeToFit()

    /// Add label to the view.
    headerView.addSubview(headerLabel)

    /// Return view.
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    /// Return the same height as the height of the label in your header view.
    return 30
}

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