可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答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:
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
}
回答5:
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)
}
回答6:
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
}
回答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
}
Note:First import the font you want to use