ios 如何将 UITableView 的部分标题居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4875391/
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 center the section headers of a UITableView
提问by
I want to center the header on the tableview, however I have 2 problems. First I don't have the correct height and second the UILabel doesn't look like the default header - font/font size/color etc... Is there a better way to center it, and/or is there a way to make it look like the default header.
我想在 tableview 上居中标题,但是我有两个问题。首先我没有正确的高度,其次 UILabel 看起来不像默认标题 - 字体/字体大小/颜色等......有没有更好的方法来居中它,和/或有没有办法让它看起来像默认标题。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
//section text as a label
UILabel *lbl = [[UILabel alloc] init];
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = @"Header";
[lbl setBackgroundColor:[UIColor clearColor]];
return lbl;
}
采纳答案by vdaubry
You must also implement tableView:heightForHeaderInSection to adjust your header height :
您还必须实现 tableView:heightForHeaderInSection 来调整标题高度:
In the UITableViewDelegate Protocol Reference doc you find :
在 UITableViewDelegate 协议参考文档中,您会发现:
tableView:viewForHeaderInSection:
Discussion
The returned object, for example, can be a UILabel or UIImageView object. The table view automatically adjusts the height of the section header to accommodate the returned view object. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.
tableView:viewForHeaderInSection:
讨论
例如,返回的对象可以是 UILabel 或 UIImageView 对象。表格视图会自动调整节标题的高度以适应返回的视图对象。 此方法仅在 tableView:heightForHeaderInSection: 也实现时才能正常工作。
For centering the Header label you must specify the label frame before setting its aligment to center. For getting the standart font, use SystemFontOfSize:
为了将标题标签居中,您必须在将其对齐方式设置为居中之前指定标签框架。要获得标准字体,请使用 SystemFontOfSize:
Also beware you are creating a memory leakyou are supposed to return an autoreleased view
还要注意你正在创建一个内存泄漏你应该返回一个自动发布的视图
Try something like this :
尝试这样的事情:
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:12];
Hope this helps, Vincent
希望这会有所帮助,文森特
回答by Brackston Mayhall
this should solve your issue. Tested in xcode 6 / ios8
这应该可以解决您的问题。在 xcode 6 / ios8 中测试
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextAlignment:NSTextAlignmentCenter];
return [sectionTitles objectAtIndex:section];
}
回答by Volker Voecking
If you target iOS6 and lateryou don't have to provide your own header view if you just want to center the header title.
Just implement
如果您面向 iOS6 及更高版本,如果您只想将标题标题居中,则不必提供自己的标题视图。
只需执行
- tableView:willDisplayHeaderView:forSection:
- tableView:willDisplayHeaderView:forSection:
and set the textAligment property of the label:
并设置标签的 textAligment 属性:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
回答by Tal Haham
Volker's answer in Swift:
Volker 在 Swift 中的回答:
If you target iOS6 and later you don't have to provide your own header view if you just want to center the header title.
如果您面向 iOS6 及更高版本,如果您只想将标题标题居中,则不必提供自己的标题视图。
Just implement
只需执行
- tableView:willDisplayHeaderView:forSection:
and set the textAligment property of the label:
并设置标签的 textAligment 属性:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = .Center
}
}
回答by Abo3atef
To make this method being called you should first implement
要调用此方法,您应该首先实现
titleForHeaderInSection
then tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
method will be called
然后tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
方法将被调用
Swift Solution:
迅捷解决方案:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = Localize().isRTL ? .Right : .Left
headerView.textLabel?.text = partsDataSource[section]
headerView.textLabel?.textColor = UIColor ( red: 0.0902, green: 0.2745, blue: 0.2745, alpha: 1.0 )
headerView.textLabel?.font = UIFont(name: UIDecorator.sharedInstance.PRIMARY_FONT, size: 14.0)
headerView.contentView.backgroundColor = UIDecorator.sharedInstance.currentTheme.lightShadeColor
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return " "
}
回答by Chris
Here's my version, you'll need to take a screenshot of the normal header background, and save a 1px wide slice of it as 'my_head_bg.png' and add it to the project. This way, it'll look just exactly normal:
这是我的版本,您需要截取普通标题背景的屏幕截图,并将其 1px 宽的切片保存为“my_head_bg.png”并将其添加到项目中。这样,它看起来就完全正常了:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
lbl.text = @"My Centered Header";
lbl.textColor = [UIColor whiteColor];
lbl.shadowColor = [UIColor grayColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
lbl.alpha = 0.9;
return lbl;
}
回答by Prabh
Try this
尝试这个
UILabel *tableHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[tableHeader setText:@"This is header"];
[tableHeader setTextAlignment:UITextAlignmentCenter];
UITableView *newTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 280, 200) style:UITableViewStylePlain];
[newTable setBackgroundColor:[UIColor clearColor]];
[newTable setTableHeaderView:tableHeader];
self.view = newTable;
回答by Gustavo Baiocchi Costa
In Xamarin:
在 Xamarin 中:
// Center Header
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (headerView.GetType() == typeof(UITableViewHeaderFooterView))
{
UITableViewHeaderFooterView tableViewHeaderFooterView = (UITableViewHeaderFooterView)headerView;
tableViewHeaderFooterView.TextLabel.TextAlignment = UITextAlignment.Center;
}
}
回答by Aurum Aquila
To change height, implement this method:
要更改高度,请实现此方法:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
return 15;
}
I am not really sure about the default header thing - that should be way easier than it is. Maybe someone has a nice background image you can use with a white font. But for the centering thing, try this:
我不太确定默认标题的事情 - 这应该比它容易得多。也许有人有一个漂亮的背景图片,您可以使用白色字体。但对于居中的事情,试试这个:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,15)];
label.textAlignment = UITextAlignmentCenter
label.text = @"Text Should Be Centered";