tech savvy
tech savvy

Reputation: 1435

UITableViewCell to display long texts in cell.textLabel

If I have a long text I have to increase the cell size to fit the text.

When I assign: cell.textLabel.text = @"my string" , if the string is long it gets truncated.

How can I display the text in two or more rows for this case? I am using UITableViewCell only and not subclassing it anywhere. Is there some code to display long texts using cell.textLabel directly? I am not talking about adding a seperate view to cell.

Upvotes: 1

Views: 1611

Answers (3)

Nelson
Nelson

Reputation: 13

This is for Swift.

cell.textLabel?.numberOfLines = 10 /// or the number you like.

Upvotes: 0

manjulapra
manjulapra

Reputation: 85

Multiple lines can be shown using

cell.textLabel.LineBreakMode = NSLineBreakByWordWrapping

Upvotes: 0

Sam B
Sam B

Reputation: 27618

cell.textLabel will not allow you to line break a string into two lines. What you will have to do is customize it add your own UILabel to UITableViewCell and define its parameters.

Here's a working code that you can add to your TableView.

//define labelValue1 in your .h file

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"Inside cellForRowAtIndexPath");

    static NSString *CellIdentifier = @"Cell";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
    {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        labelValue1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 100)]; //adjust label size and position as needed
        labelValue1.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size: 23.0];
        labelValue1.textColor = [UIColor whiteColor];
        labelValue1.textAlignment = NSTextAlignmentCenter;
        labelValue1.numberOfLines = 2; //note: I said number of lines need to be 2
        labelValue1.backgroundColor = [UIColor clearColor];
        labelValue1.adjustsFontSizeToFitWidth = YES;
        labelValue1.tag = 100;
        [cell.contentView addSubview:labelValue1];


    }
    else
    {
        labelValue1 = (UILabel *) [cell viewWithTag:100];
    }


    // Set up the cell.
    NSString *str1 = [arryData3 objectAtIndex:indexPath.row];
    labelValue1.text = str1;


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;

}

Upvotes: 1

Related Questions