简体   繁体   中英

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. I have a UITableView and the cell is created by 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. 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. I've also tried both of the commented out lines (though searches suggest that sizeToFit might actually reset numberOfLines to 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. This is a mistake, because it means that the cell has not assumed its final size. It is much better to call dequeueReusableCellWithIdentifier:forIndexPath: . This means that the cell will actually have the height that you are giving it in heightForRowAtIndexPath: . You should then be able to set the height of the label successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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