2017-02-02 10 views
1

グループ化されたテーブルビューがあり、セクションヘッダーのフォントスタイルを変更する必要があります。私は試しました:グループ化されたuitableviewのヘッダーフォントを変更するには?

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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight)) 
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0) 
    let label = UILabel.init(frame: CGRect.init(x: 20, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight)) 
    label.font = UIFont(name: "Futura", size: 11) 
    label.textColor = UIColor.blue 
    view.addSubview(label) 
    return view 
} 

私は一度に上記のコードを試してみましたが、何も動作していないようです。助けてください。ありがとう!

+1

例外が発生するか、デフォルトのUIFontをレンダリングするだけですか? – Emptyless

+0

こんにちは@Emptyless、それはデフォルトのUIFontをレンダリングします。 – Number45

答えて

2

私はそれを試みます。できます。

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


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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight)) 
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0) 
    view.textLabel?.text = "Hello" 
    return view 
} 

または

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    // 
    let label = header.viewWithTag(1000) as? UILabel 
    label?.font = UIFont(name: "Futura", size: 15) 
} 


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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight)) 
    view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
    view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0) 
    let label = UILabel.init(frame: CGRect.init(x: 20, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight)) 
    label.font = UIFont(name: "Futura", size: 11) 
    label.textColor = UIColor.blue 
    view.addSubview(label) 
    label.text = "Hello" 
    label.tag = 1000 
    return view 
} 

私はそれがあなたのために有用であろう願っています。

+0

優秀、第2の作品!私はあなたの助けに感謝します! :) – Number45

関連する問題