2015-07-13 21 views
16

Chciałbym zmienić typ czcionki i rozmiar czcionki nagłówka sekcji w kontroler widoku tabeli.Swift ios 8 zmienić tytuł czcionki w widoku tabeli

Mój kod:

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

Ale to nie działa. Jakieś pomysły?

+0

Futura plik czcionki muszą być tam wewnątrz folderu projektu – iRiziya

+0

ta czcionka jest włączone. tekst komórki miał również tę czcionkę, – Ghost108

+0

ok .. następnie spróbuj powyższego kodu w metodzie 'viewForHeaderInSection'. I weź etykietę dla tekstu nagłówka 'var title = UILabel() title.font = UIFont (name:" Futura ", rozmiar: 38)! title.textColor = UIColor.lightGrayColor() 'a następnie' header.addSubview (title) ' – iRiziya

Odpowiedz

7

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 
    } 
+0

To nie działa - czy możesz wyjaśnić wpis w nagłówku? –

+0

Ustawia tekst etykiety na tytuł sekcji widoku tabeli. Używam go w aplikacji na żywo z systemem Swift 3. Czy masz wcześniej ustawiony tytuł? – AdamVanBuskirk

+0

Musisz się upewnić, że z jakiegoś powodu istnieje wysokość komórki - co sprawiło, że zadziałało. –

31
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() 
} 
+6

' UITableViewHeaderFooterView.textLabel' jest Właściwość tylko do odczytu. Tak więc 'header.textlabel = title' nie zadziała. – Atticus

2
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 
} 

Uwaga: Pierwszy import czcionka chcesz użyć

54
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() 
} 
+5

To rozwiązanie wydaje się być nawet czystsze niż przyjęta odpowiedź i sprawdziło się doskonale w Swift 2.0. –

6

Proste Roztwór roboczy: (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 
} 
5

znalazłem najłatwiejszy sposób polega na wykonaniu następujących czynności w kontrolerze widoku lub karcie le 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) 


} 
7

Aktualizacja dla 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 
} 
Powiązane problemy