swift ios 8在tableview中更改section的字体标题 [英] swift ios 8 change font title of section in a tableview

查看:659
本文介绍了swift ios 8在tableview中更改section的字体标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改表视图控制器中节标题的字体类型和字体大小。

I would like to change the font type and font size of a section header in a table view controller.

我的代码:

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

但这不起作用。有什么想法?

But this doesn't work. Any ideas?

推荐答案

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
    }

这篇关于swift ios 8在tableview中更改section的字体标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆