UITableView titleForHeaderInSection显示全部大写

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

我想这是苹果更新的人机接口指南的一部分。 那里的所有例子都显示在所有上限中的标题。 此外,我的iPhone上的设置中的所有部分标题全部大写。

但是,我想知道有没有办法解决这个问题。 通常情况下,如果能够提高一致性,我不会介意显示大写字母,但是当我需要在部分标题中显示人名时,显得有些尴尬。

任何人有任何想法如何绕过大写?

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

苹果UITableViewHeaderFooterView文档(指向它的链接很长,但你可以用你最喜欢的search引擎轻松find它)说,你可以访问标题视图的textLabel,而不必通过viewForHeaderInSection方法来格式化你自己的视图。

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

@property(nonatomic,只读,保留)UILabel * textLabel Discussion访问此属性中的值将导致视图创build一个默认标签,用于显示详细文本string。 如果您通过向contentView属性添加子视图来自己pipe理视图的内容,则不应该访问此属性。

根据string的大小,尽可能以最好的方式将标签大小设置为适合内容视图区域。 其尺寸也根据是否存在详细的文字标签进行调整。

通过一些额外的search,修改标签文本的最佳位置是willDisplayHeaderView方法( 如何在iOS7风格上实现viewForHeaderInSection? )。

所以,我想到的解决scheme非常简单,只是在实际上由titleForHeaderInSection设置之后,对文本标签string进行了转换:

-(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方法来修改它的外观:

 - (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”子句,因为段号也被传递给方法,所以希望它可以让你用大写字母select性地显示你的用户名/客户名。

本。

__

我停止了把有趣的sig放进去,因为我是唯一一个觉得很有趣的人。

解决scheme,我发现是添加标题在“titleForHeaderInSection”方法

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

如果你想只保留string,你可以简单地这样做:

 - (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中

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

我发现的一个解决scheme是利用UITableViewHeaderFooterView

代替

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

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

烦人的缺点是表格视图将不再自动调整为您的部分标题高度。 所以如果你的标题高度不一样,你将不得不这样做:

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

我真的不喜欢以这种方式硬编码布局信息(插图),因为它可能在未来版本中打破。 如果任何人有一个更好的解决scheme来获得/设置标题的高度,我全部耳朵。

谢谢@ Animal451。 这是更通用的解决scheme,可以处理任何types的头string。

 // 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. } 

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

最简单的解决scheme是:Swift 2.x

 func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { header.textLabel?.text = header.textLabel?.text?.capitalizedString } 

为我工作的解决scheme是创build一个简单的UILabel 实例variables作为部分标题视图。 (您的需求可能会有所不同,但对我而言,我只需要在头文件中有文本…)然后,在viewForHeaderInSection内部,根据需要设置标签,并将该标签作为标题视图返回。

然后,只要我想更新标题中的文字,我就直接更改标签的文字。

在.h文件中:

 @property (nonatomic, retain) UILabel *sectionHeaderLabel; 

然后在.m文件中:

 - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [self refreshMySectionHeaderLabel]; // I created this handy little method to update the header text } // Get the latest text for the section header... -(UILabel *) refreshMySectionHeaderLabel { // Initialise the first time. if( sectionHeaderLabel == nil ) { sectionHeaderLabel = [[UILabel alloc] initWithFrame: CGRectNull]; } // ... // May also set other attributes here (eg colour, font, etc...) // Figure out the text... sectionHeaderLabel.text = @"Your updated text here..."; return sectionHeaderLabel; } 

当我想更新我的部分标题时,我只需要调用:

 [self refreshMySectionHeaderLabel]; 

…或者你可以简单地打电话给:

 sectionHeaderLabel.text = @"The new text..."; 

注意:我只有一个部分(第0部分)。 您可能需要考虑多个部分…

使用RubyMotion / RedPotion,将其粘贴到您的TableScreen中:

  def tableView(_, willDisplayHeaderView: view, forSection: section) view.textLabel.text = self.tableView(_, titleForHeaderInSection: section) end 

我觉得发生了什么事情是在那里的文本被设置为所有上限。 这个小技巧将文本重新设置回原来的样子。 对我来说就像一个魅力!

一个class轮解决scheme,基于@Dheeraj D答案:

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

Swift 3.x:

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