繁体   English   中英

分组表视图标题中的UILabel不换行

[英]UILabel in a grouped table view's header doesn't wrap text

我有一个标签,其文本长于包含该标签的表视图标题,因此我希望根据可用宽度将标签分成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;
}

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

谢谢!

您应该实现委托方法heightForHeaderInSection

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section ==1)
        return 100;
    else
        return 0;    
}
- (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;
}

如果要支持6.0以下的iOS,请使用此选项

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

由于UILineBreakModeWordWrap现在已弃用。

否则使用这个

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

您应该将标签的大小设置为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];

}

您需要计算uilabel的高度,该高度等于字体大小*数字线

似乎换行/格式化不会在标头视图中的标签上起作用-大概是因为它从某个地方继承默认值。 因此,一个简单的解决方案是将标签放在标题视图内的视图内,而AFAIK不会违反法律。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM