【问题标题】:UILabel in a grouped table view's header doesn't wrap text分组表视图标题中的 UILabel 不换行
【发布时间】:2013-06-19 16:05:00
【问题描述】:

我有一个标签,其文本比包含它的表格视图的标题长,所以我希望根据可用宽度将标签分成 N 行。这是我的代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  if (section == 1) {
    UIView *wrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
    [wrapper setBackgroundColor:[UIColor clearColor]];

    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
    textLabel.text = NSLocalizedString(@"This is supposed to be a very long text that may fill several lines", @"");
    [textLabel setLineBreakMode:UILineBreakModeWordWrap];
    [textLabel setNumberOfLines:0];
    [wrapper addSubview:textLabel];

    return wrapper;
  }
  else
    return nil;
}

但是,标签在一行中,我看不到文本的结尾。我错过了什么?

谢谢!

【问题讨论】:

    标签: iphone ios uitableview uilabel word-wrap


    【解决方案1】:

    你应该实现委托方法heightForHeaderInSection

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if(section ==1)
            return 100;
        else
            return 0;    
    }
    

    【讨论】:

      【解决方案2】:
      - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
      {
        if (section == 1) {
      
        NSString *text = NSLocalizedString(@"This is supposed to be a very long text that may fill several lines", @"");;
      
          CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:10] constrainedToSize:CGSizeMake(self.tableView.frame.size.width, 999) lineBreakMode:NSLineBreakByWordWrapping];
          CGRect frame = CGRectZero;
          frame.size = size;
      
          UIView *wrapper = [[UIView alloc] initWithFrame:frame];
          [wrapper setBackgroundColor:[UIColor clearColor]];
      
          UILabel *textLabel = [[UILabel alloc] initWithFrame:frame];
          textLabel.text = NSLocalizedString(@"This is supposed to be a very long text that may fill several lines", @"");
          [textLabel setLineBreakMode:UILineBreakModeWordWrap];
          [textLabel setNumberOfLines:0];
          [wrapper addSubview:textLabel];
      
          return wrapper;
        }
        else
          return nil;
      }
      

      【讨论】:

        【解决方案3】:

        如果您想支持低于 6.0 的 iOS,请使用此功能

        textLabel.lineBreakMode = UILineBreakModeWordWrap;
        textLabel.numberOfLines = 0;
        

        UILineBreakModeWordWrap 现在已弃用。

        否则使用这个

        textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        textLabel.numberOfLines = 0;
        

        【讨论】:

          【解决方案4】:

          您应该将标签的大小设置为 heightForRowAtIndexPath 中的文本

          - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
              NSString *text;
              CGSize textViewSize;
              int width = 270;
              textViewSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:14.0]
                              constrainedToSize:CGSizeMake(width, FLT_MAX)
                                  lineBreakMode:UILineBreakModeCharacterWrap];
              CGFloat height = MAX(textViewSize.height, 44.0f);
              return height;
          
          }
          

          在 cellForRowAtIndexPath 中写下这个

           - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          
          
              textViewSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:14.0]
                              constrainedToSize:CGSizeMake(width, FLT_MAX)
                                  lineBreakMode:UILineBreakModeCharacterWrap];
              //  /  CGFloat height = MAX(textViewSize.height, 44.0f);
          
              //    if (!lblParameter){
              //        lblParameter = (UILabel*)[cell viewWithTag:1];}
              CGFloat height = MAX(textViewSize.height, 44.0f);
              [lblParameter setText:text];
              [lblParameter setFont:[UIFont fontWithName:@"Helvetica Neue" size:14.0]];
              [lblParameter setFrame:CGRectMake(10, 0,width,height)];
              [[cell contentView] addSubview:lblParameter];
          
          }
          

          【讨论】:

            【解决方案5】:

            你需要计算uilabel的高度这个高度等于字体大小*数字线

            【讨论】:

              【解决方案6】:

              似乎包装/格式化不适用于标题视图内的标签 - 大概是因为它从某个地方继承了默认值。因此,AFAIK 不违反任何法律的简单解决方案是将标签放在标题视图内的视图内。

              使用情节提要,将视图拖到表格视图控制器(原型单元格上方)。
              向该视图添加第二个视图。 将标签添加到第二个视图。确保视图和标签都足够高以允许多行文本。在标签的属性中,将 Lines 设置为 0,将 Line Breaks 设置为 Word Wrap。 包装现在将按预期工作。

              【讨论】:

                猜你喜欢
                • 2013-09-07
                • 1970-01-01
                • 2020-08-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-07-31
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多