如何更改分组的uitableview的标题字体?

时间:2017-02-02 23:58:24

标签: ios swift

我有一个分组表视图,想要更改节标题的字体样式。我试过了:

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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0)
    let label = UILabel.init(frame: CGRect.init(x: 20, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
    label.font = UIFont(name: "Futura", size: 11)
    label.textColor = UIColor.blue
    view.addSubview(label)
    return view
}

我一次尝试了上面的代码,但似乎没有任何效果。请帮忙。谢谢!

1 个答案:

答案 0 :(得分:3)

我试着去做。它有效。

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


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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0)
    view.textLabel?.text = "Hello"
    return view
}

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    //
    let label = header.viewWithTag(1000) as? UILabel
    label?.font = UIFont(name: "Futura", size: 15)
}


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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0)
    let label = UILabel.init(frame: CGRect.init(x: 20, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
    label.font = UIFont(name: "Futura", size: 11)
    label.textColor = UIColor.blue
    view.addSubview(label)
    label.text = "Hello"
    label.tag = 1000
    return view
}

我希望这对你有用。