【问题标题】:UITableView titleForHeaderInSection shows all capsUITableView titleForHeaderInSection 显示全部大写
【发布时间】:2013-09-25 14:50:06
【问题描述】:

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

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

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

有人知道如何绕过大写吗?

【问题讨论】:

    标签: objective-c uitableview header capitalization


    【解决方案1】:

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

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

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

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

    标签的大小以最佳方式适合内容视图区域 可能基于字符串的大小。它的大小也进行了调整 取决于是否存在详细文本标签。

    通过一些额外的搜索,修改标签文本的最佳位置是 willDisplayHeaderView 方法(建议在 How to implement `viewForHeaderInSection` on iOS7 style? 中)。

    所以,我想出的解决方案非常简单,只需在 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 方法来修改它的外观:

    - (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”子句,因为节号也会传递给该方法,因此希望它可以让您有选择地以大写单词显示您的用户/客户名称。

    【讨论】:

    • 超级!我将您的 willDisplayHeaderView 方法复制到我的代码 UITableViewController 中,它立即起作用。发送!
    • 如果你想显示一个完整的句子,因为它使每个单词都大写,它不起作用。
    • Raaah,@moon4e,要有创意!例如,title = [NSString stringWithFormat:@"%@%@", [[title substringToIndex:1] uppercaseString], [[title substringFromIndex:1] lowercaseString]];
    • 所以你需要一些更复杂的东西,比如- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,并根据你的字符串创建你的视图。
    • 我遇到了一个问题,来自viewForHeaderInSection: 的视图最初显示的是默认/未设置样式的标题,然后快速重新加载了样式版本。在 willDisplayHeaderView: 中设置视图样式可以解决问题。
    【解决方案2】:

    我找到的解决方案是在“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;
    }
    

    【讨论】:

    • 同时添加 titleForHeaderInSection 和 willDisplayHeaderView 方法有效。谢谢
    【解决方案3】:

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

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

    快速解决方案:

    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)
        }
    }
    

    【讨论】:

      【解决方案4】:

      Swift 中,

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

      【讨论】:

        【解决方案5】:

        我找到的一个解决方案是使用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;
        }
        

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

        【讨论】:

          【解决方案6】:

          感谢@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.
          
          }
          

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

          【讨论】:

            【解决方案7】:

            除了@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"
             }
            

            然后忽略-titleForHeaderInSection:

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

            【讨论】:

            • titleForHeaderInSection 仍然需要有从方法返回的标题标题,否则部分标题都是空白的。
            【解决方案8】:

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

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

            【讨论】:

            • 什么是标头对象?
            • 它是 UITableViewHeaderFooterView 对象
            • 这还不够,您需要提供真实代码才能让您的示例工作。
            【解决方案9】:

            Swift 3.x:

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

            【讨论】:

              【解决方案10】:

              斯威夫特 5

              静态 TableViewController 和故事板部分标题。 下面的代码将使用大写的第一个字母。

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

              【讨论】:

              • 这会将空格后面的单词的第一个字符大写,它本身就是 CamelCase。
              【解决方案11】:

              对我有用的解决方案是创建一个简单的UILabel Instance Variable 用作部分标题视图。 (您的需求可能不同,但对我来说,我只需要在标题中包含文本...) 然后,在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 (e.g. colour, font, etc...)
                  // Figure out the text...    
                  sectionHeaderLabel.text   =   @"Your updated text here...";
              
              
                  return sectionHeaderLabel;
              }
              

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

              [self refreshMySectionHeaderLabel];
              

              ...或者您可以简单地调用:

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

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

              【讨论】:

                【解决方案12】:

                一个班轮解决方案,基于@Dheeraj D 答案:

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

                【讨论】:

                  【解决方案13】:

                  SWIFT

                  在实现中,我发现您需要在 titleForHeaderInSectionwillDisplayHeaderView 函数中指定部分标题文本,否则标题隐藏。

                      extension ViewController: UITableViewDelegate, UITableViewDataSource {
                  
                          func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
                              let sectionHeader =  datasource[section].sectionHeader  
                  
                              // Header Title
                              return sectionHeader 
                          }
                  
                          func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                  
                              guard let header = view as? UITableViewHeaderFooterView else { return }
                              header.textLabel?.textColor = UIColor.darkGray
                              header.textLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
                              header.textLabel?.frame = header.frame
                  
                              // Header Title
                              header.textLabel?.text = datasource[section].sectionHeader
                          }
                  
                      }
                  

                  【讨论】:

                    【解决方案14】:

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

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

                    我认为正在发生的事情是那里的某处文本被设置为全部大写。这个小技巧将文本重置为原来的样子。对我来说就像一个魅力!

                    【讨论】:

                      【解决方案15】:

                      使用方法viewForHeaderInSection来创建标题标题。

                      viewDidLoad中注册一个UITableViewHeaderFooterView

                      tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HeaderFooterViewID")
                      

                      添加委托

                          override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                      
                              let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderFooterViewID")
                              var config = header?.defaultContentConfiguration()
                                  
                              let title = "My Header Title"
                              let attribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]
                              
                              config?.attributedText = NSAttributedString(string: title, attributes: attribute)
                              
                              header?.contentConfiguration = config
                              
                              return header
                          }
                      

                      你可以更多地调整属性文本,reference

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2016-12-21
                        • 2016-11-09
                        • 1970-01-01
                        • 2012-10-07
                        • 2015-02-20
                        • 1970-01-01
                        • 2014-10-12
                        • 1970-01-01
                        相关资源
                        最近更新 更多