Number45
Number45

Reputation: 149

How to change the header font of a grouped uitableview?

I have a grouped table view and want to change the font style of the section headers. I have tried:

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
}

I tried the above code one at a time and together, but nothing seems to work. Please help. Thanks!

Upvotes: 1

Views: 3172

Answers (1)

cwwise
cwwise

Reputation: 101

I try to do it. It works.

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
}

or

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
}

I hope that would be useful for you.

Upvotes: 5

Related Questions