有人可以指导我更改 UITableView 部分标题中文本的字体大小的最简单方法吗?
我使用以下方法实现了部分标题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
然后,我了解了如何使用此方法成功更改节标题高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
我使用这种方法填充了 UITableView 单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
但是,我对如何实际增加节标题文本的字体大小 - 或者就此而言字体样式 - 感到困惑?
有人可以帮忙吗?谢谢。
另一种方法是响应 UITableViewDelegate
方法 willDisplayHeaderView
。传递的视图实际上是 UITableViewHeaderFooterView
的一个实例。
下面的示例更改了字体,并将标题文本在单元格内垂直和水平居中。请注意,您还应该响应 heightForHeaderInSection
以在表格视图的布局中考虑对标题高度的任何更改。 (也就是说,如果您决定在此 willDisplayHeaderView
方法中更改标题高度。)
然后,您可以响应 titleForHeaderInSection
方法以使用不同的部分标题重用此配置的标题。
Objective-C
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
header.textLabel.font = [UIFont boldSystemFontOfSize:18];
CGRect headerFrame = header.frame;
header.textLabel.frame = headerFrame;
header.textLabel.textAlignment = NSTextAlignmentCenter;
}
斯威夫特 1.2
(注意:如果您的视图控制器是 UITableViewController
的后代,则需要将其声明为 override func
。)
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.redColor()
header.textLabel.font = UIFont.boldSystemFontOfSize(18)
header.textLabel.frame = header.frame
header.textLabel.textAlignment = NSTextAlignment.Center
}
斯威夫特 3.0
如果您的标题视图不是 UITableViewHeaderFooterView,此代码还可以确保应用程序不会崩溃:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.textLabel?.textColor = UIColor.red
header.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
header.textLabel?.frame = header.bounds
header.textLabel?.textAlignment = .center
}
不幸的是,您可能必须覆盖它:
在 Objective-C 中:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
在斯威夫特:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
尝试这样的事情:
在 Objective-C 中:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont boldSystemFontOfSize:18];
myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
在斯威夫特:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let myLabel = UILabel()
myLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 20)
myLabel.font = UIFont.boldSystemFont(ofSize: 18)
myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
let headerView = UIView()
headerView.addSubview(myLabel)
return headerView
}
tableView:heightForHeaderInSection:
中执行标题高度的计算,这可能很麻烦。
虽然 mosca1337 的答案是正确的解决方案,但请小心使用该方法。对于文本长度超过一行的标题,您必须在 tableView:heightForHeaderInSection:
中执行标题高度的计算,这可能很麻烦。
一个更受欢迎的方法是使用外观 API:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont boldSystemFontOfSize:28]];
这将更改字体,同时仍让表格自行管理高度。
为获得最佳结果,将表格视图子类化,并将其添加到包含链(在 appearanceWhenContainedIn:
中),以确保仅针对特定表格视图更改字体。
- tableView:viewForHeaderInSection:
返回自定义视图,对吗?在这种情况下,字体可以在那里设置。这就是@mosca1337 的解决方案所做的。
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
对于 iOS 7,我使用这个,
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.font = [UIFont boldSystemFontOfSize:10.0f];
header.textLabel.textColor = [UIColor orangeColor];
}
这是带有标题大小调整的 Swift 3.0 版本
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let header = view as? UITableViewHeaderFooterView {
header.textLabel!.font = UIFont.systemFont(ofSize: 24.0)
header.textLabel!.textColor = UIColor.orange
}
}
斯威夫特 3:
仅调整大小的最简单方法:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
if let textlabel = header.textLabel {
textlabel.font = textlabel.font.withSize(15)
}
}
斯威夫特 2.0:
用完全可定制的 UILabel 替换默认节标题。
实现 viewForHeaderInSection,如下所示:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
if sectionTitle == "" {
return nil
}
let title: UILabel = UILabel()
title.text = sectionTitle
title.textColor = UIColor(red: 0.0, green: 0.54, blue: 0.0, alpha: 0.8)
title.backgroundColor = UIColor.clearColor()
title.font = UIFont.boldSystemFontOfSize(15)
return title
}
更改默认标题(保留默认值)。
实现 willDisplayHeaderView,如下所示:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.backgroundView?.backgroundColor = UIColor.blueColor()
view.textLabel!.backgroundColor = UIColor.clearColor()
view.textLabel!.textColor = UIColor.whiteColor()
view.textLabel!.font = UIFont.boldSystemFontOfSize(15)
}
}
请记住:如果您使用的是静态单元格,由于 UITableView 的顶部,第一个节标题比其他节标题填充得更高;解决这个问题:
实现 heightForHeaderInSection,如下所示:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30.0 // Or whatever height you want!
}
在这里,你必须遵循在这里写一些方法。 #斯威夫特 5
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as? UITableViewHeaderFooterView
header?.textLabel?.font = UIFont.init(name: "Montserrat-Regular", size: 14)
header?.textLabel?.textColor = .greyishBrown
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 26
}
祝你好运
使用此方法,您还可以设置字体大小、字体样式和页眉背景。有两种方法
第一种方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.backgroundView.backgroundColor = [UIColor darkGrayColor];
header.textLabel.font=[UIFont fontWithName:@"Open Sans-Regular" size:12];
[header.textLabel setTextColor:[UIColor whiteColor]];
}
第二种方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
// myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont fontWithName:@"Open Sans-Regular" size:12];
myLabel.text = [NSString stringWithFormat:@" %@",[self tableView:FilterSearchTable titleForHeaderInSection:section]];
myLabel.backgroundColor=[UIColor blueColor];
myLabel.textColor=[UIColor whiteColor];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
Swift 4 版本的 Leo Natan answer 是
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
如果您想设置自定义字体,您可以使用
if let font = UIFont(name: "font-name", size: 12) {
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = font
}
斯威夫特 2:
正如 OP 所要求的,只调整大小,而不是将其设置为系统粗体字体或其他任何东西:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView, textLabel = headerView.textLabel {
let newSize = CGFloat(16)
let fontName = textLabel.font.fontName
textLabel.font = UIFont(name: fontName, size: newSize)
}
}
这是我使用 swift 5 的解决方案。
如上一篇文章所示,要完全控制标题部分视图,您需要在控制器中使用 tableView(:viewForHeaderInsection::) 方法。但是,还有更进一步的步骤:为了提高性能,苹果建议不要每次都生成新视图,而是重用标题视图,就像重用表格单元格一样。这是通过方法 tableView.dequeueReusableHeaderFooterView(withIdentifier: )。但是我遇到的问题是,一旦您开始使用此重用功能,字体将无法按预期运行。其他的东西,比如颜色,对齐都很好,但只是字体。有一些讨论,但我让它像下面这样工作。
问题是 tableView.dequeueReusableHeaderFooterView(withIdentifier:) 不像 tableView.dequeneReuseCell(:) 总是返回一个单元格。如果没有可用的,前者将返回 nil。即使它返回一个重用的标题视图,它也不是你原来的类类型,而是一个 UITableHeaderFooterView。所以你需要在自己的代码中做判断和行动。基本上,如果它是 nil,则获得一个全新的标题视图。如果不为零,则强制施放以便您可以控制。
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let reuse_header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "yourHeaderID")
if (reuse_header == nil) {
let new_sec_header = YourTableHeaderViewClass(reuseIdentifier:"yourHeaderID")
new_section_header.label.text="yourHeaderString"
//do whatever to set color. alignment, etc to the label view property
//note: the label property here should be your custom label view. Not the build-in labelView. This way you have total control.
return new_section_header
}
else {
let new_section_header = reuse_section_header as! yourTableHeaderViewClass
new_sec_header.label.text="yourHeaderString"
//do whatever color, alignment, etc to the label property
return new_sec_header}
}
header.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
和/或header.detailTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
以及所需的其他步骤(参见此处:captechconsulting.com/blog/john-szumski/…)