简体   繁体   中英

swift ios 8 change font title of section in a tableview

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

My code:

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?

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

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
    }

Updated for Swift 3

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header = view as? UITableViewHeaderFooterView
    header?.textLabel?.font = UIFont(name: "Futura", size: 12) // change it according to ur requirement
    header?.textLabel?.textColor = UIColor.red // change it according to ur requirement
}

I found the easiest way is to do the following in your View Controller or Table View Controller.

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "FontName", size: 14)


}

Simple working solution: (Swift 2.0)

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

            //Create label and autoresize it
            let headerLabel = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 2000))
            headerLabel.font = UIFont(name: "Avenir-Light", size: 30)
            headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
            headerLabel.sizeToFit()

            //Adding Label to existing headerView
            let headerView = UIView()
            headerView.addSubview(headerLabel)

            return headerView
}
 func tableView(tableView: UITableView,
        viewForHeaderInSection section: Int) -> UIView? {
                let hView = UIView(frame: CGRectMake(0, 0, tableView.frame.width, 44))
                hView.backgroundColor = UIColor.whiteColor()
                let hLabel = UILabel(frame: CGRectMake(15, 2, 30, 44))
                hLabel.font = UIFont(name: "YOUR_FONT_NAME", size: 30)
                hLabel.textColor = kiExtremeOrange
                hLabel.text = alphabets[section]
                hView.addSubview(hLabel)
                return hView
}

Note: First import the font you want to use

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

Xamarin/ C#

Based on Atticus' answer using willDisplayHeaderView :

public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
    var header = headerView as UITableViewHeaderFooterView;
    header.TextLabel.Font = UIFont.FromName("Futura", header.TextLabel.Font.PointSize);
}

Please note that Xamarin is a cross-platform development tool for building iOS Apps using the C# language. The code above is C# and will not work in Xcode. This answer is just for developers who use Xamarin but still want to achieve what the OP also wanted.

There're several ways for you to archive your question by customizing the view in your storyboard, xib, tableViewCell, or you can write it by code.

After your customization completed, you can use

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

    return yourHeader
}

Updated for Xcode 11, Swift 5

The top voted answers didn't work for me, here's what did :

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

    /// Create the view.
    let headerView = UIView()
    headerView.backgroundColor = .clear

    /// Create the label that goes inside the view.
    let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView.bounds.size.width, height: 30))
    headerLabel.font = UIFont(name: "myFont", size: 12)
    headerLabel.textColor = .white
    headerLabel.text = "myHeaderName"
    headerLabel.sizeToFit()

    /// Add label to the view.
    headerView.addSubview(headerLabel)

    /// Return view.
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    /// Return the same height as the height of the label in your header view.
    return 30
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM