How to wrap text in a UITable cell in Iphone?

My iPhone UITable app has so many text cells. The text size is larger than the device width. How can one wrap text in a cell and display the text on different lines, or split it into lines?

+2


source to share


1 answer


You can add UILabel as a subset of a cell in

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

      



method. Then set the lineBreakMode and numberOfLines properties to your liking. The code should look something like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tID];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:tID] autorelease];
 }
else{
       UIView *tView = [cell viewWithTag:100];
    [tView removeFromSuperview];
}
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 90, 50)];
label.tag = 100;
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = @"Putyourverylongcelltexthere";
[cell addSubview:label];
[label release];

      

+2


source







All Articles