swift ios 8在tableview中更改section的字体标题

我想在表视图控制器中更改节标题的字体类型和字体大小。

我的代码:

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

但这不起作用。 有任何想法吗?

斯威夫特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 } 
 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进行了更新

 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 } 

简单的工作解决方案:(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 } 

我发现最简单的方法是在View Controller或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) } 
  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 } 

注意: 首先导入要使用的字体