UITableView titleForHeaderInSection显示所有大写字母

gbr*_*stg 44 header objective-c capitalization uitableview

我使用titleForHeaderInSection来显示UITableView部分的标题.它适用于iOS6 SDK,但iOS7 SDK在所有CAPS中都显示了标题.

我想这是Apple更新的人机界面指南的一部分; 所有示例都显示所有大写的标题.此外,我的iPhone上的设置中的所有部分标题都是全部大写.

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

任何人都知道如何绕过大写?

Ani*_*451 70

是的,我们遇到了类似的问题,我自己的解决方案如下:

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

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

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

根据字符串的大小,标签的大小可以最佳方式适合内容视图区域.其大小也根据是否存在详细文本标签进行调整.

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

因此,我提出的解决方案非常简单,只需对文本标签字符串进行转换,之后它实际上是由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];
}
Run Code Online (Sandbox Code Playgroud)

然后只需调用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];
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在其中放入自己的"if"或"switch"子句,因为节号也会传递给方法,因此希望它允许您有选择地以大写单词显示您的用户/客户端名称.

本.

__

我不再把有趣的sig放进去了.因为我是唯一一个发现它们很有趣的人.


use*_*599 21

我找到的解决方案是在"titleForHeaderInSection"方法中添加Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   return @"Table Title";
}
Run Code Online (Sandbox Code Playgroud)

然后调用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;
}
Run Code Online (Sandbox Code Playgroud)


tob*_*ann 17

如果你想保持字符串不变,你可以简单地这样做:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)


Jef*_*Neo 15

斯威夫特,

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


Jos*_*Lin 5

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

代替

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"some title";
}
Run Code Online (Sandbox Code Playgroud)

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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