ios 如何在分组类型 UITableView 中更改标题的字体颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7105747/
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
How to change font color of the title in grouped type UITableView?
提问by Confused
I have a grouped type table view and it looks pretty cool.
我有一个分组类型的表格视图,它看起来很酷。
But, if I change the background color of the table to black, the titles becomes unclear.
但是,如果我将表格的背景颜色更改为黑色,标题就会变得不清楚。
Is it possible to change the font color and its styles so that I can make it more readable? Should I implement the tableView:viewForHeaderInSection:
method?
是否可以更改字体颜色及其样式以使其更具可读性?我应该实施该tableView:viewForHeaderInSection:
方法吗?
采纳答案by Confused
Yes... It works great now!
是的......它现在很好用!
I created tableView:viewForHeaderInSection:
method and created a UIView
我创建了tableView:viewForHeaderInSection:
方法并创建了一个 UIView
UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
Then i created a UILabel & set the text values & colors to the label. Then i added the label to the view
然后我创建了一个 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];
So my tableView:viewForHeaderInSection:
method looks like...
所以我的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;
}
We should add tableView:heightForHeaderInSection:
method for providing some space to the title.
我们应该添加tableView:heightForHeaderInSection:
为标题提供一些空间的方法。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
回答by Code Commander
If you just need to change the color or font on the header, use tableView: willDisplayHeaderView: forSection:
. Here is an example in swift:
如果您只需要更改标题上的颜色或字体,请使用tableView: willDisplayHeaderView: forSection:
. 这是一个快速的例子:
Swift v5:
斯威夫特 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
}
}
Original:
原来的:
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()
}
}
回答by Adriano Lucas
To use the default coordinates and the sections in TableView, with white-color font and shadow:
要使用默认坐标和 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;
}
回答by Jhaliya
The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection:
instead.
表格视图为节标题标题使用固定字体样式。如果您想要不同的字体样式,请tableView:viewForHeaderInSection:
改为在委托方法中返回自定义视图(例如 UILabel 对象)。
So use the below method and return your custom view (UILabel) with your choice of font.
因此,请使用以下方法并使用您选择的字体返回您的自定义视图 (UILabel)。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
回答by Nitesh Borad
if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
}
回答by Zoltan Vinkler
Swift 4.2
斯威夫特 4.2
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white