Dynamische Zellenhöhen unter iOS |

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {static UIFont *labelFont;static CGRect textFrame;static CGFloat extraHeight;if(!labelFont){// verwendete Zelle auswählenmyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];// verwendete Schriftart auswählenlabelFont = cell.cellLabel.font;// frame der ZelleCGRect cellFrame = cell.frame;// frame des labelstextFrame = cell.cellLabel.frame;// extra PufferextraHeight = cellFrame.size.height-textFrame.size.height;}// der anzuzeigende TextNSString* text = [tableItems objectAtIndex:indexPath.row];// NSAttributedString erstellenNSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];// LineBreakMode auf "BreakByWordWrapping" setzenNSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];[attributedString setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, attributedString.length)];// Schriftart auswählen[attributedString setAttributes:@{NSFontAttributeName:labelFont} range:NSMakeRange(0, attributedString.length)];// Hier wird die Höhe berechnet - mit max. Höhe 300CGSize expectedSize = [attributedString boundingRectWithSize:CGSizeMake(textFrame.size.width, 300) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;return expectedSize.height+extraHeight;}

Den darzustellenden Text auswählen

NSString* text = [tableItems objectAtIndex:indexPath.row];

Schriftart des darzustellenden Labels festlegen

[attributedString setAttributes:@{NSFontAttributeName:labelFont} range:NSMakeRange(0, attributedString.length)];

Höhe berechnen

CGSize expectedSize = [attributedString boundingRectWithSize:CGSizeMake(textFrame.size.width, 300) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;

--

--

Hallo, wir sind tapthis. Ihre Agentur für App Entwicklung.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
tapthis

tapthis

Hallo, wir sind tapthis. Ihre Agentur für App Entwicklung.