Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 748
Ben Avatar asked Jul 28 '16 00:07

Ben


People also ask

How to add header and footer in tableView in swift?

To create a basic header or footer with a text label, override the tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.

What is Uitableview in Swift?

A view that presents data using rows in a single column.

What is header in Swift?

For output message, the basic header identifies the application through which SWIFT has processed the message. The basic header also identifies the type of output data, the receiving logical terminal, and (if required) the output session number and output sequence number.


2 Answers

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"
like image 86
Godspeed Avatar answered Nov 14 '22 20:11

Godspeed


Swift 4

use this delegates:

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

            label.textAlignment = .right
            label.textColor = .white
            label.backgroundColor = .clear
            label.font = UIFont.systemFont(ofSize: 20)

            return label
        }()
        return label
    }

and don't forget to set height for header:

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

hope this help.

like image 37
moraei Avatar answered Nov 14 '22 22:11

moraei