【问题标题】:swift ios 8 change font title of section in a tableviewswift ios 8更改表格视图中部分的字体标题
【发布时间】:2015-10-01 14:56:34
【问题描述】:

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

我的代码:

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

但这不起作用。有什么想法吗?

【问题讨论】:

  • Futura 字体文件必须在您的项目文件夹中
  • 包含此字体。单元格文本也有这种字体
  • ok..然后在viewForHeaderInSection方法中尝试上面的代码。并为标题文本标签var title = UILabel() title.font = UIFont(name: "Futura", size: 38)! title.textColor = UIColor.lightGrayColor() 然后header.addSubview(title)
  • 一切正常,上面的代码是正确的-我把代码放在了错误的swift文件中-.-

标签: ios swift uitableview fonts


【解决方案1】:
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()
}

【讨论】:

  • UITableViewHeaderFooterView.textLabel 是只读属性。所以header.textlabel = title 不起作用。
【解决方案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
}

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

【讨论】:

    【解决方案3】:
    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()
    }
    

    【讨论】:

    • 这个解决方案似乎比公认的答案更干净,并且在 Swift 2.0 中对我来说效果很好。
    • 如果UITableViewHeaderFooterView had a function call inject(font: UIFont, textColor: UIColor) 和视图没有被强制解包,则更简洁的版本是
    【解决方案4】:

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

      我发现最简单的方法是在您的视图控制器或表视图控制器中执行以下操作。

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

      【讨论】:

        【解决方案6】:

        斯威夫特 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
            }
        

        【讨论】:

        • 这不起作用 - 你能解释一下 headerlabel 文本条目吗?
        • 它将标签的文本设置为表格视图的部分标题。我在运行 Swift 3 的 Live 应用中使用它。你事先设置了标题吗?
        • 由于某种原因,您需要确保单元格有一个高度 - 这使它工作
        • 好点,我也设置了高度。我将其添加到上面的答案中以供将来的访问者使用。
        • 问题是你在这一行中设置标签的高度等于表格的高度: let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView. bounds.size.width, height: tableView.bounds.size.height))
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          Xamarin/C#

          基于 Atticus 使用 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 是一个跨平台开发工具,用于使用 C# 语言构建 iOS 应用程序。上面的代码是 C#,不能在 Xcode 中工作。此答案仅适用于使用 Xamarin 但仍希望实现 OP 也想要的开发人员。

          【讨论】:

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

            【讨论】:

            • 见上面@Atticus 的回答!
            【解决方案10】:

            您可以通过多种方式来归档您的问题,方法是自定义您的故事板、xib、tableViewCell 中的视图,或者您可以通过代码编写它。

            自定义完成后,您可以使用

            func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                let yourHeader = .....
            
                return yourHeader
            }
            

            【讨论】:

              【解决方案11】:

              已针对 Xcode 11、Swift 5 更新

              票数最高的答案对我不起作用,这就是原因

              func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
              
                  /// Create the view.
                  let headerView = UIView()
                  headerView.backgroundColor = .clear
              
                  /// Create the label that goes inside the view.
                  let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: tableView.bounds.size.width, height: 30))
                  headerLabel.font = UIFont(name: "myFont", size: 12)
                  headerLabel.textColor = .white
                  headerLabel.text = "myHeaderName"
                  headerLabel.sizeToFit()
              
                  /// Add label to the view.
                  headerView.addSubview(headerLabel)
              
                  /// Return view.
                  return headerView
              }
              
              func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                  /// Return the same height as the height of the label in your header view.
                  return 30
              }
              

              【讨论】:

                猜你喜欢
                • 2018-12-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-10-18
                • 2015-07-26
                • 1970-01-01
                • 2012-01-19
                • 2016-07-21
                相关资源
                最近更新 更多