Table查看 Header/Footer 分组中的字体 Table (Swift)

TableView Header/Footer Font in Grouped Table (Swift)

我将 tableView 从 Plain 更改为 Grouped,这样 header/footer 就不会漂浮在 table 上方并固定在 [=35= 的顶部和底部].这很简单,但现在我设置的字体样式格式不起作用。奇怪的是 header/footer 的所有其他格式似乎都有效。对正在发生的事情和我遗漏的任何想法表示赞赏!

代码如下:

// Setup format of the header
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let title = UILabel()
    title.font = UIFont(name: "Avenir Book", size: 12)
    title.textColor = UIColor.whiteColor()


    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 50/255, alpha: 1)
    header.textLabel!.font = title.font
    header.textLabel?.textColor = title.textColor
    header.textLabel?.numberOfLines = 0
    header.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    header.textLabel?.textAlignment = NSTextAlignment.Center

}

在 Plain table 中,以上所有都很好用,看起来像这样:

但是,当我更改为分组 table 时,除了字体样式之外,所有格式似乎都显示了:

我对 ALL CAPS 的来源感到困惑。

我尝试实施 的解决方案,但也无法使其工作。感谢您的想法!

假设您在此方法中提供了章节标题的字符串:

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

但是在分组表视图中,该字符串被更改为全部大写。要删除全部大写,请尝试在 willDisplayHeaderView 方法中添加以下两行之一:

header.textLabel?.text = header.textLabel!.text!.capitalizedString
header.textLabel?.text = header.textLabel!.text!.lowercaseString

第一个将每个单词的第一个字母大写,第二个将所有单词小写。如果你不想要其中任何一个,你可以直接添加字符串:

header.textLabel?.text = "Here are a few options for you. Select to learn more"

Swift 4

使用这个代表:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label: UILabel = {
            let label = UILabel()

            label.textAlignment = .right
            label.textColor = .white
            label.backgroundColor = .clear
            label.font = UIFont.systemFont(ofSize: 20)

            return label
        }()
        return label
    }

并且不要忘记为 header 设置高度:

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

希望对您有所帮助。