swift ios 8 change font title of section in a tableview

前端 未结 11 1597
夕颜
夕颜 2021-01-31 08:29

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, willDi         


        
相关标签:
11条回答
  • 2021-01-31 09:07

    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)
    
    
    }
    
    0 讨论(0)
  • 2021-01-31 09:12
    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()
    }
    
    0 讨论(0)
  • 2021-01-31 09:15

    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
    }
    
    0 讨论(0)
  • 2021-01-31 09:18

    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
    }
    
    0 讨论(0)
  • 2021-01-31 09:19
    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()
    }
    
    0 讨论(0)
提交回复
热议问题