objective-c UITableView titleForHeaderInSection 显示全部大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18912980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:00:41  来源:igfitidea点击:

UITableView titleForHeaderInSection shows all caps

objective-cuitableviewheadercapitalization

提问by gbroekstg

I am using titleForHeaderInSection to show a header for a UITableView section. It worked fine with the iOS6 SDK, but the iOS7 SDK shows the header in all CAPS.

我正在使用 titleForHeaderInSection 来显示 UITableView 部分的标题。它适用于 iOS6 SDK,但 iOS7 SDK 在所有大写字母中显示标题。

I guess it's part of Apple's updated Human Interface Guidelines; all examples in there show headers in all caps. Also, all section headers in Settings on my iPhone are in all caps.

我猜这是 Apple 更新的人机界面指南的一部分;那里的所有示例都以大写字母显示标题。此外,我 iPhone 上设置中的所有部分标题都是大写的。

However, I wonder if there is a way around that. Normally, I wouldn't mind showing caps if that improves consistency, but when I need to show people's names in a section header, it's a bit awkward.

但是,我想知道是否有办法解决这个问题。通常,如果可以提高一致性,我不介意显示大写字母,但是当我需要在部分标题中显示人名时,这有点尴尬。

Anybody any idea how to get around to capitalization?

有人知道如何解决大写吗?

回答by Animal451

Yes, we had very much a similar problem to this, and my own solution was as follows:

是的,我们遇到了与此非常相似的问题,我自己的解决方案如下:

The Apple UITableViewHeaderFooterView documentation (the link to it is irritatingly long but you can find it easily with your favourite search engine) says you can access the textLabel of the header view without having to format your own view via viewForHeaderInSection method.

Apple UITableViewHeaderFooterView 文档(指向它的链接很长,但您可以使用自己喜欢的搜索引擎轻松找到它)说您可以访问标题视图的 textLabel ,而无需通过 viewForHeaderInSection 方法格式化您自己的视图。

textLabel A primary text label for the view. (read-only)

@property(nonatomic, readonly, retain) UILabel *textLabel Discussion Accessing the value in this property causes the view to create a default label for displaying a detail text string. If you are managing the content of the view yourself by adding subviews to the contentView property, you should not access this property.

The label is sized to fit the content view area in the best way possible based on the size of the string. Its size is also adjusted depending on whether there is a detail text label present.

textLabel 视图的主要文本标签。(只读)

@property(nonatomic, readonly, retain) UILabel *textLabel 讨论访问此属性中的值会导致视图创建默认标签以显示详细信息文本字符串。如果您通过向 contentView 属性添加子视图来自己管理视图的内容,则不应访问此属性。

标签的大小根据字符串的大小以可能的最佳方式适应内容视图区域。它还根据是否存在详细文本标签来调整其大小。

With some additional searching the best place to modify the label text was the willDisplayHeaderView method (suggested in How to implement `viewForHeaderInSection` on iOS7 style?).

通过一些额外的搜索,修改标签文本的最佳位置是 willDisplayHeaderView 方法(在如何在 iOS7 样式上实现`viewForHeaderInSection` 中建议?)。

So, the solution I came up with is pretty simple and just does a transformation of the text label string, after it's actually been set by titleForHeaderInSection:

所以,我想出的解决方案非常简单,只是在 titleForHeaderInSection 实际设置后,对文本标签字符串进行了转换:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        //I have a static list of section titles in SECTION_ARRAY for reference.
        //Obviously your own section title code handles things differently to me.
    return [SECTION_ARRAY objectAtIndex:section];
}

and then simply call the willDisplayHeaderView method to modify how it looks:

然后简单地调用 willDisplayHeaderView 方法来修改它的外观:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
        UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
        tableViewHeaderFooterView.textLabel.text = [tableViewHeaderFooterView.textLabel.text capitalizedString];
    }
}

You can put in your own 'if' or 'switch' clauses in there as the section number is also passed to the method, so hopefully it'll allow you to show your user/client name in capitalised words selectively.

您可以在其中放入自己的“if”或“switch”子句,因为部分编号也传递给方法,因此希望它允许您有选择地以大写字母显示您的用户/客户名称。

回答by user3540599

solution which i found is add Title in "titleForHeaderInSection" method

我发现的解决方案是在“titleForHeaderInSection”方法中添加标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   return @"Table Title";
}

and then call the willDisplayHeaderView method to Update :

然后调用 willDisplayHeaderView 方法更新:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.textLabel.textColor = [UIColor darkGrayColor];
    header.textLabel.font = [UIFont boldSystemFontOfSize:18];
    CGRect headerFrame = header.frame;
    header.textLabel.frame = headerFrame;
    header.textLabel.text= @"Table Title";
    header.textLabel.textAlignment = NSTextAlignmentLeft;
}

回答by tobihagemann

If you want to just keep the string as it is, you could simply do this:

如果您只想保持字符串原样,您可以简单地执行以下操作:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]] && [self respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
        UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
        headerView.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    }
}

Swift solution:

迅捷解决方案:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView, self.respondsToSelector(Selector("tableView:titleForHeaderInSection:")) {
        headerView.textLabel?.text = self.tableView(tableView, titleForHeaderInSection: section)
    }
}

回答by Jeffrey Neo

In Swift,

Swift 中

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel.text = "My_String"
}

回答by Joseph Lin

One solution I found is to utilize UITableViewHeaderFooterView.

我发现的一种解决方案是使用UITableViewHeaderFooterView.

Instead of

代替

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"some title";
}

Do

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *identifier = @"defaultHeader";
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];
    }
    headerView.textLabel.text = @"some title";
    return headerView;
}

The annoying downside is that the table view will no longer automatically adjust the section header height for you. So if your header heights varies, you'll have to do something like this:

令人讨厌的缺点是表格视图将不再自动为您调整节标题高度。因此,如果您的标题高度不同,则必须执行以下操作:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    id headerAppearance = [UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil];
    UIFont *font = [headerAppearance font];
    CGFloat viewWidth = CGRectGetWidth(tableView.frame);
    CGFloat xInset = 10;
    CGFloat yInset = 5;
    CGFloat labelWidth = viewWidth - xInset * 2;
    CGSize size = [sectionInfo.name sizeWithFont:font constrainedToSize:CGSizeMake(labelWidth, MAXFLOAT)];
    return size.height + yInset * 2;
}

I really don't like hard-coding layout information (the inset) this way, as it might break in the future version. If anyone has a better solution to get/set the header height, I'm all ears.

我真的不喜欢这种硬编码布局信息(插图),因为它可能会在未来的版本中中断。如果有人有更好的解决方案来获取/设置标题高度,我会全力以赴。

回答by moon4e

Thanks @Animal451. This is more generic solution that would work with any type of header string.

谢谢@Animal451。这是更通用的解决方案,适用于任何类型的标头字符串。

// We need to return the actual text so the header height gets correctly calculated
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.headerString;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setText:self.headerString];       //String in the header becomes uppercase. Workaround to avoid that.

}

To avoid duplication the header string can be declared anywhere else. I have done it in the -viewDidLoad method.

为了避免重复,头字符串可以在其他任何地方声明。我已经在 -viewDidLoad 方法中完成了。

回答by KIO

In addition to @Animal451's post. For swift 3 you can use

除了@Animal451 的帖子。对于swift 3,您可以使用

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  guard section == 0 ,
    let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView
    else { return }

  tableViewHeaderFooterView.textLabel?.text = "Your awesome string"
 }

And then ignore - titleForHeaderInSection:

然后忽略 - titleForHeaderInSection:

Keep in mind that this code is for 1st section only. If you want to go through all of your sections, you'll need to add support for them

请记住,此代码仅适用于第一部分。如果您想浏览所有部分,则需要添加对它们的支持

回答by Matt Bearson

Swift 3.x:

斯威夫特 3.x:

if let headerView = view as? UITableViewHeaderFooterView {
    headerView.textLabel?.text? = headerView.textLabel?.text?.capitalized ?? ""
}

回答by Dheeraj D

Simplest Solution for was below: Swift 2.x

最简单的解决方案如下:Swift 2.x

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        header.textLabel?.text = header.textLabel?.text?.capitalizedString
}

回答by Federico Zanetello

One liner solution, based on @Dheeraj D answer:

一种基于@Dheeraj D 答案的班轮解决方案:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  (view as? UITableViewHeaderFooterView)?.textLabel?.text = (view as? UITableViewHeaderFooterView)?.textLabel?.text?.capitalized
}