我有一个 UITableView 有几个标题,但字体大小比我想要的大得多:
我在系统首选项中看到 Apple 使用大写的部分标题使用较小的字体大小,底部对齐如下:
我知道我可以使用 viewForHeaderInSection 创建我自己的布局,但我的问题是有一个简单的选项可以调用这个标准的操作系统样式,而无需重新发明轮子?
对于较小的字体大小和底部对齐(如 Apple 使用大写的节标题的系统首选项),我们必须使用 UITableView 的委托方法。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.lightGray
let sectionLabel = UILabel(frame: CGRect(x: 8, y: 28, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
sectionLabel.font = UIFont(name: "Helvetica", size: 12)
sectionLabel.textColor = UIColor.black
sectionLabel.text = "NETWORK SETTINGS"
sectionLabel.sizeToFit()
headerView.addSubview(sectionLabel)
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此方法来设置自定义字体系列和章节标题的对齐方式。
viewForHeaderInSection 方法仅用于设置默认字体系列和节标题的对齐方式。
希望对你有帮助。
| 归档时间: |
|
| 查看次数: |
4891 次 |
| 最近记录: |