我有一个分组的表格视图,看起来很酷。
但是,如果我将表格的背景颜色更改为黑色,则标题会变得不清楚。
是否可以更改字体颜色及其样式,以便使其更具可读性?我应该实施tableView:viewForHeaderInSection:
方法吗?
答案 0 :(得分:43)
使用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;
}
答案 1 :(得分:40)
如果您只需要更改标题上的颜色或字体,请使用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()
}
}
答案 2 :(得分:22)
是的......现在效果很好!
我创建了tableView:viewForHeaderInSection:
方法并创建了一个UIView
UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
然后我创建了一个UILabel&amp;设置文本值&amp;颜色到标签。然后我将标签添加到视图
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;
}
答案 3 :(得分:2)
表格视图对章节标题使用固定的字体样式。如果您想要不同的字体样式,请在委托方法tableView:viewForHeaderInSection:
中返回自定义视图(例如,UILabel对象)。
因此,请使用以下方法并使用您选择的字体返回自定义视图(UILabel)。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
答案 4 :(得分:2)
if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}
答案 5 :(得分:0)
Swift 4.2
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white