Mon text est long de deux lignes en mode portrait. Quand je passe en mode paysage, il ne tient qu'à une seule ligne. J'utilise des cellules staticview view via un storyboard; comment puis-je resize la rangée pour l'ajuster confortablement?
L'écran est un écran de connection.
Utilisez 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 { NSSsortingng *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 ssortingng 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:NSSsortingngDrawingUsesLineFragmentOrigin atsortingbutes:@{NSFontAtsortingbuteName :font,NSParagraphStyleAtsortingbuteName:[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 ssortingng height } else { // return height from the storyboard return [super tableView:tableView heightForRowAtIndexPath:indexPath]; } }
EDIT : Ajouté pour le support
de > and < ios7
et comme la méthode sizeWithFont
est obsolète dans iOS 7.0
J'ai eu du succès avec une implémentation un peu plus simple. Tant que votre vue de table statique a des contraintes appropriées sur les cellules, vous pouvez requestr au système de la dimensionner pour vous:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let cell = self.tableView(self.tableView, cellForRowAtIndexPath: indexPath) let height = ceil(cell.systemLayoutSizeFittingSize(CGSizeMake(self.tableView.bounds.size.width, 1), withHorizontalFittingPriority: 1000, verticalFittingPriority: 1).height) return height }