Note that there are some explanatory texts on larger screens.

plurals
  1. POcell height and line break
    text
    copied!<p>I am Updating a iOS5 project to storybaord and compatible to run with iOS6 / iOS7. my table view have 4 sections and each section have data of different length which may or may not be displayed as a single line.</p> <p>the old code is some thing like this and I get a warning for the statement </p> <pre><code>CGSize size = [productToShow.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping]; </code></pre> <p><strong>warning</strong> <code>sizeWithFont:constrainedToSize:lineBreakMode:is first deprecated in iOS7</code></p> <p><strong>so I have tried using</strong><code>boundingRectWithSize:options:attributes:context:</code> as suggested by the compiler.</p> <p>but the code seems to be more complicated: apple wouldn't be making simpler things difficult. if some one can see my codes and suggest the best method or a better and simple method it would be of grate help to me and the community.</p> <p><strong>iOS5 code with XIBs</strong></p> <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); self.lblHidden.text = tempPointStr; CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping]; if (size.height &lt; 50.0f) { return 50.0f; } else { return size.height; } } elseif(indexPath.section == 1) { ... } else { ... } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { static NSString *tempPoint = @"ProductPointsCell"; ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint]; if (cell == nil) { NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ProductPointsCell_iPad" owner:self options:nil]; cell = [nib objectAtIndex:0]; cell.showsReorderControl = NO; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor=[UIColor clearColor]; cell.lblPoint.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f]; } NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; cell.lblPoint.text = tempPointStr; self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); self.lblHidden.text = tempPointStr; CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping]; if (size.height &lt; 50.0f) { cell.lblPoint.frame = CGRectMake(58, 0, 945, 50.0f); } else { cell.lblPoint.frame = CGRectMake(58, 0, 945, size.height); } return cell; } elseif(indexPath.section == 1) { ... } else { ... } } </code></pre> <p>where as the same code in :</p> <p><strong>iOS6/7 codes with storybaord</strong></p> <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { NSString *tempPointStr = (self.shortDescArray)[indexPath.row]; self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); self.lblHidden.text = tempPointStr; CGSize constraint = CGSizeMake(945, 9999.0f); NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName]; NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:self.lblHidden.text attributes:attributes]; CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; CGSize size = rect.size; if (size.height &lt; 50.0f) { return 50.0f; } else { return size.height; } } elseif(indexPath.section == 1) { ... } else { ... } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { static NSString *tempPoint = @"ProductPointsCell"; ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint]; cell.showsReorderControl = NO; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor=[UIColor clearColor]; cell.pointLbl.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f]; [cell.pointLbl setLineBreakMode:NSLineBreakByWordWrapping]; [cell.pointLbl setNumberOfLines:0]; NSString *tempPointStr = (self.shortDescArray)[indexPath.row]; cell.pointLbl.text = tempPointStr; CGSize constraint = CGSizeMake(945,9999.0f); NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:cell.pointLbl.text attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}]; CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; CGSize size = rect.size; if (size.height &lt; 50.0f) { cell.pointLbl.frame = CGRectMake(58, 0, 945, 50.0f); } else { cell.pointLbl.frame = CGRectMake(58, 0, 945, size.height); } return cell; } elseif(indexPath.section == 1) { ... } else { ... } } </code></pre> <p>The purpose of these codes are to make the data displayed in a single line when it fits within a single line and when it doesn't the cell height is adjusted and the data is displayed as a multiline cell.</p> <p>Also correct me if I should keep the autolayout on or off and should I set any parameters in the storyboard while converting.</p> <p>if some one can suggest a better/faster and easier way please do so with examples.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload