提问者:小点点

UITableView titleForHeaderInSection显示所有大写


我正在使用titleForHeaderInCourt来显示UITableView部分的标头。它与iOS6 SDK配合使用很好,但iOS7 SDK在所有大写字母中显示标头。

我想这是苹果更新的《人机界面指南》的一部分;这里的所有示例都以大写形式显示标题。此外,iPhone上“设置”中的所有部分标题都是大写的。

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

有人知道如何进行资本化吗?


共3个答案

匿名用户

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

Apple UITableViewHeaderFooterView文档(它的链接很长,但你可以用你最喜欢的搜索引擎很容易地找到它)说你可以访问标题视图的textLabel,而不必通过viewForHeaderInSection方法格式化你自己的视图。

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

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

根据字符串的大小,标签的大小以尽可能适合内容视图区域。其大小也会根据是否存在详细信息文本标签进行调整。

通过一些额外的搜索,修改标签文本的最佳位置是willDisplayHeaderView方法(在如何在iOS7 style上实现‘viewforheaderinsection’中建议)。).

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

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

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

- (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”子句,因为部分编号也会传递给该方法,因此希望它允许您有选择地以大写字母显示您的用户/客户名称。

匿名用户

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

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

然后调用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;
}

匿名用户

在Swift中,

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