iDev
iDev

Reputation: 2421

Wrap Text in a UITableView in iOS

I want to wrap text of the cells in the UITableView. I am using the following code but the alignment of cells isnt coming proper now how to I change the Height of the cell dynamically? I noticed that there is an inbuilt method -(CGFloat) cellHeightForRow in tableView delegate but I can I take in text and set height dynamically since I have text of variable length in my JSON data

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil) {  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];  
    }  
    [cell.textLabel sizeToFit];
    cell = [[[UITableViewCell alloc]
             initWithStyle: UITableViewCellStyleSubtitle
             reuseIdentifier: @"UITableViewCell"] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *name = [person valueForKey:@"name"];
    cell.detailTextLabel.text = [person valueForKey:@"time"];
    return cell; 
}

This is what I have tried so far :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 
    NSString *cellText    =[person valueForKey:@"text"];
    UIFont *cellFont      = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize      = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    int buffer  = 10;
    return labelSize.height + buffer;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";  

    UITableViewCell *cell = [commentView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 6;
        cell.textLabel.font          = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
        [cell.textLabel setMinimumFontSize:13.0];
        [cell.textLabel setAdjustsFontSizeToFitWidth:NO];
    } 

    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *personName = [person valueForKey:@"text"];
        cell.textLabel.text = personName;
   // cell.detailTextLabel.text = [person valueForKey:@"date"];

    return cell; 
}

Still the output looks too clumpsy and tight

Upvotes: 4

Views: 4775

Answers (2)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Inside your cellForRowAtIndexPath: function. The first time you create your cell:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font          = [UIFont fontWithName:@"HelveticaNeue" size:21.0];
}

You'll notice that also I set the number of lines for the label to 0. This lets it use as many lines as it needs.

You also need to specify how large your UITableViewCell will be, so do that in your heightForRowAtIndexPath function:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText    = @"some text which is part of cell display";
    UIFont *cellFont      = [UIFont fontWithName:@"HelveticaNeue" size:21.0];    
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize      = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    int buffer  = 10;
    return labelSize.height + buffer;
}

I added an extra 10 to my returned cell height because I like a little buffer around my text.

UPDATE: If the output is looking too tight & clumpsy then do this -

[cell.textLabel setMinimumFontSize:13.0];
[cell.textLabel setAdjustsFontSizeToFitWidth:NO];

This should fix your problem.

Upvotes: 15

jbat100
jbat100

Reputation: 16827

To change the height of your cell you need to change its frame in the cellForRowAtInexPath and make sure the label has the right auto sizing flags.

Upvotes: 0

Related Questions