xcode UITableView titleForSection 字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2450861/
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
UITableView titleForSection font
提问by camilo
A quick question, for a quick answer (since I'm not finding any):
一个快速问题,快速回答(因为我没有找到):
Is there a way to change the font of the section's title (given by titleForSection) in iPhone?
有没有办法在 iPhone 中更改部分标题的字体(由 titleForSection 给出)?
Thanks a lot!
非常感谢!
回答by camilo
Thanks Jasarien! You were absolutely right.
谢谢贾萨里恩!你完全正确。
I leave my code here to help someone with the same problem:
我将代码留在这里以帮助遇到相同问题的人:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = @"Just a title";
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 284, 23);
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"Helvetica" size:14];
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[view autorelease];
[view addSubview:label];
return view;
}
回答by Jasarien
You'll have to use the viewForHeaderInSection:
method and provide your own view. Luckily this can be a UILabel with a specified font, so you can do it quite easily.
您必须使用该viewForHeaderInSection:
方法并提供您自己的视图。幸运的是,这可以是具有指定字体的 UILabel,因此您可以轻松完成。
回答by ArunGJ
You have to override
你必须覆盖
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
to give required height for the section title. Else it will overlap with the cell.
为部分标题提供所需的高度。否则它将与单元格重叠。
回答by EricWasTaken
viewForHeaderInSection might work fine... but here's an alternative that works fine for me (Xcode 5 and IOS7 target):
viewForHeaderInSection 可能工作正常......但这里有一个对我来说工作正常的替代方案(Xcode 5 和 IOS7 目标):
// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];
// Text Color & Alignment
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
[header.textLabel setTextAlignment:NSTextAlignmentCenter];
// Text Font
UIFont *saveFont = header.textLabel.font;
[header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];
// Another way to set the background color
// Note: does not preserve gradient effect of original heade!r
// header.contentView.backgroundColor = [UIColor blackColor];
}