Swift ios 8 изменить название раздела раздела в виде таблицы

Я хотел бы изменить тип шрифта и размер шрифта заголовка раздела в контроллере табличного представления.

Мой код:

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)!
}

Но это не работает. Любые идеи?

Ответы

Ответ 1

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
    }

Ответ 2

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()
}

Ответ 3

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()
}

Ответ 4

Обновлено для 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
}

Ответ 5

Я нашел самый простой способ сделать следующее в вашем контроллере View или 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)


}

Ответ 6

Простое рабочее решение: (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
}

Ответ 7

 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
}

Примечание: Сначала импортируйте шрифт, который вы хотите использовать

Ответ 8

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

Ответ 9

Xamarin/C#

Основано на ответе Аттикуса с использованием 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);
}

Обратите внимание, что Xamarin - это кроссплатформенный инструмент разработки для создания приложений iOS с использованием языка С#. Код выше является С# и не будет работать в Xcode. Этот ответ только для разработчиков, которые используют Xamarin, но все еще хотят достичь того, чего тоже хотел OP.