在 iOS 中,tableView:titleForHeaderInSection: 返回的標題文字默認左對齊,若需居中顯示,必須實現 tableView:viewForHeaderInSection: 返回自定義視圖。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textAlignment = NSTextAlignmentCenter; // 關鍵:居中對齊
headerLabel.font = [UIFont boldSystemFontOfSize:14];
headerLabel.textColor = [UIColor whiteColor];
switch (section) {
case 0: headerLabel.text = NSLocalizedString(@"more_titlehead_one", nil); break;
case 1: headerLabel.text = NSLocalizedString(@"more_titlehead_two", nil); break;
default: headerLabel.text = NSLocalizedString(@"more_titlehead_three", nil); break;
}
return headerLabel;
}
// 設置 Header 高度(可選)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0; // 根據字體大小調整
}
- (void)viewDidLoad {
[super viewDidLoad];
// 全局設置 UITableViewHeaderFooterView 外觀
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor whiteColor]];
// 注意:無法直接通過 appearance 設置 textAlignment
}
此方案無法直接實現文字居中,不推薦用於此需求。
// CustomHeaderView.h
@interface CustomHeaderView : UITableViewHeaderFooterView
@property (nonatomic, strong) UILabel *titleLabel;
@end
// CustomHeaderView.m
@implementation CustomHeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:self.titleLabel];
[NSLayoutConstraint activateConstraints:@[
[self.titleLabel.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
[self.titleLabel.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor]
]];
}
return self;
}
@end
// 使用
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:@"CustomHeader"];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CustomHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CustomHeader"];
header.titleLabel.text = [self titleForSection:section];
return header;
}
viewForHeaderInSection: 後,titleForHeaderInSection: 將被忽略heightForHeaderInSection: 返回合適高度,否則可能顯示異常dequeueReusableHeaderFooterViewWithIdentifier: 復用 Header 視圖contentView.backgroundColor 或 Header 視圖的 backgroundViewfunc tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 14)
label.textColor = .white
label.backgroundColor = .clear
label.text = self.titleForSection(section)
return label
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}