0

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

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

谢谢!

4

6 回答 6

0

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

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

于 2014-05-02T17:22:01.957 回答
0

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

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

由于 UILineBreakModeWordWrap 现在已弃用。

否则使用这个

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
于 2013-06-23T08:54:39.437 回答
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];

}
于 2013-07-31T09:45:36.620 回答
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;
}
于 2013-06-22T18:15:19.490 回答
0

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

于 2013-08-26T11:05:16.567 回答
0

您应该实现委托方法heightForHeaderInSection

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section ==1)
        return 100;
    else
        return 0;    
}
于 2013-06-22T17:56:47.003 回答