0

In my application I have few views with longer text. In my detail view I have been imported text from database and for some short texts it works but for longer it make me problem. When I take smaller font it shows me all text so it loads it from database good but make some other problem. My code for that part is:

case RestaurantItemTypeDescription : {
            cell = [tableView dequeueReusableCellWithIdentifier:@"Restaurant Description"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Restaurant Description"];
            }
            cell.imageView.image = nil;

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            CGRect rectLabel = CGRectMake(cell.frame.origin.x+20, cell.frame.origin.y+10, cell.frame.size.width-20, cell.frame.size.height-20);
            UILabel* label = [[UILabel alloc] initWithFrame:rectLabel];

            label.lineBreakMode = UILineBreakModeWordWrap;
            label.numberOfLines = 0;
            label.font = [UIFont fontWithName:@"Helvetica" size:13.0]; //Same font used for calculating height
            label.text = item.itemText;
            label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [cell addSubview:label];
//          [label release];
            //NSLog(@"description=%@", item.itemText);
            cell.accessoryType = UITableViewCellAccessoryNone;
            break;
        }

I have autoresize atribute but it seams it doesn't work.

Can someone please tell me what to do? I was searching the web and it seams that there is no restriction about size of text in view. Thanks...

Akosha
  • 471
  • 1
  • 10
  • 24

2 Answers2

2

You'll need to adjust the height of the label AND the height of the cell to account for the longer text.

There's a decent guide on how to do it here.

Nathan Gaskin
  • 1,334
  • 9
  • 32
2

Autoresize attribute is only used to resize when the size of the view is changed like when the orientation changes from portrait to landscape or when you invoke setFrame:

You need to resize the UILabel to make the label large enough to show all the text. To do that you have to calculate how big the UILabel must be. This question covers various options for doing that.

Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • i implemented - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and CGSize withinsize = CGSizeMake(tableView.frame.size.width-20, 1900); the last atribute gives me size and by changing it i change size of field. It is not perfect but do things for me... :) – Akosha Nov 24 '11 at 14:58