【发布时间】:2011-10-29 15:55:18
【问题描述】:
我有一个分组类型的表格视图,它看起来很酷。
但是,如果我将表格的背景颜色更改为黑色,标题就会变得不清楚。
是否可以更改字体颜色及其样式以使其更具可读性?我应该实现tableView:viewForHeaderInSection: 方法吗?
【问题讨论】:
标签: ios iphone uitableview fonts
我有一个分组类型的表格视图,它看起来很酷。
但是,如果我将表格的背景颜色更改为黑色,标题就会变得不清楚。
是否可以更改字体颜色及其样式以使其更具可读性?我应该实现tableView:viewForHeaderInSection: 方法吗?
【问题讨论】:
标签: ios iphone uitableview fonts
如果您只需要更改标题的颜色或字体,请使用tableView: willDisplayHeaderView: forSection:。以下是 swift 中的示例:
Swift v5:
override public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.backgroundView?.backgroundColor = UIColor.blue
view.textLabel?.backgroundColor = UIColor.clear
view.textLabel?.textColor = UIColor.white
}
}
原文:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.backgroundView?.backgroundColor = ThemeBlue
view.textLabel.backgroundColor = UIColor.clearColor()
view.textLabel.textColor = UIColor.whiteColor()
}
}
【讨论】:
斯威夫特 4.2
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white
【讨论】:
是的...现在效果很好!
我创建了tableView:viewForHeaderInSection: 方法并创建了一个 UIView
UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
然后我创建了一个 UILabel 并将文本值和颜色设置为标签。然后我将标签添加到视图中
UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
titleLabel.text = @"<Title string here>";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[customTitleView addSubview:titleLabel];
所以我的tableView:viewForHeaderInSection: 方法看起来像......
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
titleLabel.text = @"<Title string here>";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[customTitleView addSubview:titleLabel];
return customTitleView;
}
我们应该添加tableView:heightForHeaderInSection: 方法来为标题提供一些空间。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
【讨论】:
if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}
【讨论】:
使用默认坐标和 TableView 中的部分,带有白色字体和阴影:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 8, 320, 20);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(-1.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIView *view = [[UIView alloc] init];
[view addSubview:label];
return view;
}
【讨论】:
表格视图对节标题使用固定字体样式。如果您想要不同的字体样式,请改为在委托方法tableView:viewForHeaderInSection: 中返回自定义视图(例如,UILabel 对象)。
所以使用下面的方法并返回您的自定义视图 (UILabel) 以及您选择的字体。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
【讨论】: