简体   繁体   English

多行UILabel

[英]Multi-line UILabel

I've looked over older questions and tried all the suggestions, but still cannot seem to get a multi-line UILabel to work. 我查看了较早的问题并尝试了所有建议,但似乎仍然无法使用多行UILabel。 I have a UITableView and the cell is created by tableView:cellForRowAtIndexPath: 我有一个UITableView并且该单元格由tableView:cellForRowAtIndexPath:创建tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    NSString *fieldValue    = [self fieldValueAtIndexPath:indexPath];
    NSString *fieldName     = [self fieldNameAtIndexPath:indexPath];
    NSString *title         = [[self appDelegate] displayNameForFieldName:fieldName];
    Field fieldCode         = [[self appDelegate] fieldCodeForFieldName:fieldName];
    DetailCell *cell        = nil;
    NSString *identifier    = nil;

    BOOL isNotes            = [fieldName caseInsensitiveCompare:@"Notes"] == NSOrderedSame;

    switch( isNotes ) {
    case NO:
    {
        identifier                  = @"DetailCell";
        cell                        = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
        NSInteger rows              = [self heightForText:fieldValue andFont:[self textFont] andWidth:cell.value.frame.size.width] / _oneRowSize.height;
        cell.value.text             = fieldValue;
        cell.name.text              = [title lowercaseString];
        cell.name.numberOfLines     = MAX( 1, rows );
        cell.value.numberOfLines    = cell.name.numberOfLines;
        break;
    }
    case YES:
    {
        cell                        = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCellNotes" forIndexPath:indexPath];
        // cell                        = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCellNotes"];
        cell.value.text             = @"This is a very long line of text which should take up several lines";
        cell.name.text              = [title lowercaseString];
        cell.value.numberOfLines    = 5; // No more than 5 lines of text
        cell.value.backgroundColor  = [UIColor purpleColor];
        cell.value.lineBreakMode    = NSLineBreakByWordWrapping;
        cell.value.frame            = CGRectMake(cell.value.frame.origin.x, cell.value.frame.origin.y, 180, 70);
        [cell.value sizeThatFits:CGSizeMake(180., 70.)];
        break;
    }
    }
cell.fieldName = fieldName;
return cell;
}

The height in the table view is defined like so 表格视图中的高度定义如下

    - (CGFloat) tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
    {
        NSString *fieldName     = [self fieldNameAtIndexPath:indexPath];
        CGFloat height          = 0.0;

        if([fieldName isEqualToString:@"Notes"])
        {
                height = 70.;
        }
        else if([fieldName isEqualToString:@"Image"])
        {
                height = 100.;
        };

        return height;
    }

which makes the cell large enough to hold a 3-line label. 这使得单元格足够大,可以容纳3行标签。 However when the cell appears the label is only one line (shown by the background being purple). 但是,当单元格出现时,标签只有一行(背景为紫色)。

The tableview uses prototype cells, and I've also tried to set it to numberOfLines=5 and WordWrapping, but that didn't change the effects either. tableview使用原型单元格,并且我也尝试将其设置为numberOfLines = 5和WordWrapping,但这也没有改变效果。 I've also tried both of the commented out lines (though searches suggest that sizeToFit might actually reset numberOfLines to 1). 我还尝试了两条注释行(尽管搜索表明sizeToFit可能实际上将numberOfLines重置为1)。

I wonder what I've missed. 我想知道我错过了什么。 I can't see any other place where the it might be overridden. 我看不到其他任何可能覆盖它的地方。

Thanks. 谢谢。

You are calling dequeueReusableCellWithIdentifier: to create your cell. 您正在调用dequeueReusableCellWithIdentifier:创建您的单元格。 This is a mistake, because it means that the cell has not assumed its final size. 这是一个错误,因为这意味着该单元尚未采用其最终大小。 It is much better to call dequeueReusableCellWithIdentifier:forIndexPath: . 最好调用dequeueReusableCellWithIdentifier:forIndexPath: This means that the cell will actually have the height that you are giving it in heightForRowAtIndexPath: . 这意味着该单元格实际上将具有您在heightForRowAtIndexPath:中给定的高度。 You should then be able to set the height of the label successfully. 然后,您应该能够成功设置标签的高度。

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

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