Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

ios - Dynamic height for static table cells with wrapping labels?

My text is two lines long in portrait mode. When I switch to landscape mode, it fits into a single line. I'm using static tableview cells via a storyboard; how can I resize the row to fit snugly?

The screen is a signin screen.

  • The first cell contains some explanation text
  • The second is a text field for entering the account name
  • The third is a secure text field for entering the password
  • The fourth (and last) cell contains the signin button. The return key on the keyboard submits the form or switches the focus as appropriate
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use UITableView's heightForRowAtIndexPath :

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   int topPadding = 10;
   int bottomPadding = 10;
   float landscapeWidth = 400;
   float portraitWidth = 300;

   UIFont *font = [UIFont fontWithName:@"Arial" size:22];

   //This is for first cell only if you want for all then remove below condition
   if (indexPath.row == 0) // for cell with dynamic height
   {
      NSString *strText = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label  
     if(landscape)//depends on orientation
     {
       CGSize maximumSize = CGSizeMake(landscapeWidth, MAXFLOAT); // change width and height to your requirement
     }
     else //protrait
     {
       CGSize maximumSize = CGSizeMake(portraitWidth, MAXFLOAT); // change width and height to your requirement
     }

     //dynamic height of string depending on given width to fit
     CGSize textSize = CGSizeZero;
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
     {
        NSMutableParagraphStyle *pstyle = [NSMutableParagraphStyle new];
        pstyle.lineBreakMode = NSLineBreakByWordWrapping;

        textSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil] size];
     }
     else // < (iOS 7.0)
     {
        textSize = [strText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping] 
     }

     return (topPadding+textSize.height+bottomPadding) // caculate on your bases as u have string height
   }
   else
   {
       // return height from the storyboard
       return [super tableView:tableView heightForRowAtIndexPath:indexPath];
   }
 }

EDIT : Added for support for > and < ios7 and as sizeWithFont method is deprecated in iOS 7.0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...