Ben Ben - 1 year ago 248
Swift Question

TableView Header/Footer Font in Grouped Table (Swift)

I changed a tableView from Plain to Grouped so that the header/footer does not float over the table and stays anchored to the top and bottom of the table. That was straightforward, but now the font style formatting that I setup is not working. Strangely all other formatting of the header/footer seems to be working though. Any thoughts on what is going and what I am missing are appreciated!

Code below:

// Setup format of the header
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

let title = UILabel()
title.font = UIFont(name: "Avenir Book", size: 12)
title.textColor = UIColor.whiteColor()

let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 50/255, alpha: 1)
header.textLabel!.font = title.font
header.textLabel?.textColor = title.textColor
header.textLabel?.numberOfLines = 0
header.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
header.textLabel?.textAlignment = NSTextAlignment.Center


In a Plain table all of the above works great and looks like this:

enter image description here

However, when I change to Grouped table all of the formatting seems to show up except for the font style this this:

I am puzzled about where the ALL CAPS is coming from

I am puzzled about where the ALL CAPS is coming from.

I tried to implement the solution from this question/answer, but could not get it to work either. Thanks for your ideas!

Answer Source

Assuming that you provided the string for the section title in this method:

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

But in a grouped tableview, that string is changed to all caps. To remove the all caps, try adding one of these two lines in the willDisplayHeaderView method:

header.textLabel?.text = header.textLabel!.text!.capitalizedString
header.textLabel?.text = header.textLabel!.text!.lowercaseString

The first one will capitalize the first letter of every word and the second will make everything lowercase. If you don't want either of those you could add the string directly:

header.textLabel?.text = "Here are a few options for you. Select to learn more"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download