swift ios 8 更改表格视图中部分的字体标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31381762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift ios 8 change font title of section in a tableview
提问by Ghost108
I would like to change the font type and font size of a section header in a table view controller.
我想更改表视图控制器中节标题的字体类型和字体大小。
My code:
我的代码:
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)!
}
But this doesn't work. Any ideas?
但这不起作用。有任何想法吗?
采纳答案by AdamVanBuskirk
Swift 3
斯威夫特 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
}
回答by Atticus
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()
}
回答by Jayesh Miruliya
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()
}
回答by iAj
Updated for Swift 3
为 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
}
回答by mooncitizen
I found the easiest way is to do the following in your View Controller or 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)
}
回答by Phil
Simple working solution: (Swift 2.0)
简单的工作解决方案:(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
}
回答by Sumit Oberoi
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
}
Note:First import the font you want to use
注意:首先导入你要使用的字体
回答by Prasann
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView,
forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.font = UIFont(name: "Futura", size: 14)
}
回答by Mark Moeykens
Xamarin/C#
Xamarin/C#
Based on Atticus' answer using willDisplayHeaderView
:
基于 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);
}
Please note that Xamarin is a cross-platform development tool for building iOS Apps using the C# language. The code above is C# and will not work in Xcode. This answer is just for developers who use Xamarin but still want to achieve what the OP also wanted.
请注意,Xamarin 是一个跨平台开发工具,用于使用 C# 语言构建 iOS 应用程序。上面的代码是 C#,在 Xcode 中不起作用。此答案仅适用于使用 Xamarin 但仍希望实现 OP 也想要的开发人员。
回答by Ly Boung
There're several ways for you to archive your question by customizing the view in your storyboard, xib, tableViewCell, or you can write it by code.
您可以通过多种方式通过自定义故事板、xib、tableViewCell 中的视图来存档您的问题,或者您可以通过代码编写它。
After your customization completed, you can use
自定义完成后,您可以使用
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let yourHeader = .....
return yourHeader
}