UITableView titleForHeaderInSection显示所有大写字母 [英] UITableView titleForHeaderInSection shows all caps

查看:547
本文介绍了UITableView titleForHeaderInSection显示所有大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用titleForHeaderInSection来显示UITableView部分的标题.它在iOS6 SDK上运行良好,但iOS7 SDK在所有CAPS中都显示了标头.

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.

我猜这是Apple更新的人机界面指南的一部分;此处的所有示例均显示所有大写字母的标题.另外,iPhone上设置"中的所有小节标题均大写.

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.

但是,我想知道是否有解决办法.通常,我不介意显示大写字母是否可以提高一致性,但是当我需要在节标题中显示人们的姓名时,会有点尴尬.

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.

有人知道如何大写吗?

推荐答案

是的,我们有一个非常类似的问题,我自己的解决方案如下:

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

Apple UITableViewHeaderFooterView文档(其链接令人讨厌,但您可以使用自己喜欢的搜索引擎轻松找到它)说,您可以访问标题视图的textLabel,而不必通过viewForHeaderInSection方法格式化自己的视图.

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.

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

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

@property(非原子,只读,保留)UILabel * textLabel讨论 访问此属性中的值会使视图创建一个 用于显示详细文本字符串的默认标签.如果您正在管理 通过将子视图添加到contentView来自己查看视图的内容 属性,则不应访问此属性.

@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.

通过一些额外的搜索,修改标签文本的最佳位置是willDisplayHeaderView方法(在

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?).

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

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:

-(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];
}

,然后只需调用willDisplayHeaderView方法即可修改其外观:

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

- (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];
    }
}

您可以在其中输入自己的'if'或'switch'子句,因为节号也传递给该方法,因此希望它可以让您有选择地用大写字母显示您的用户/客户端名称.

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.

这篇关于UITableView titleForHeaderInSection显示所有大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆