I need to calculate the height for a UITableViewCell with includes multiple rows of wrapping text. So far I couldn't find a 100% exact way to do this. Furthermore the technique I currently use relies heavily on fixed values:
我需要計算包含多行包裝文本的UITableViewCell的高度。到目前為止,我找不到100%准確的方法來做到這一點。此外,我目前使用的技術在很大程度上依賴於固定值:
NSString *cellText;
cellText = @"Very long multi line text in this String ...";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 32;
Fixed values used in just in these few lines:
僅在以下幾行中使用的固定值:
字體類型(Helvetica)
CGSize,寬度(280.0f)
我發現需要隨機填充值(32)
Is there a nice way to rewrite this in a way that all these fixed values are fetched and calculated dynamically?
有沒有一種很好的方法可以通過動態獲取和計算所有這些固定值的方式重寫它?
That would help a lot to make the layout more responsive and the code reusable going forward. I appreciate any best practices.
這將有助於使布局更具響應性並且代碼可以重復使用。我很欣賞任何最佳實踐。
1
Your code seems good but u missed is cell appropriate calculation.U need to add label's topmost postion and label's bottommost postion in cell(parentView).
你的代碼似乎很好,但你錯過了細胞適當的計算。我需要在單元格(parentView)中添加標簽的最頂層位置和標簽的最底部位置。
Just calculate like this
就像這樣計算
return labelSize.height + 2*yourLabelInCell.frame.origin.y //here label's topmost postion and label's bottommost postion in cell added
Label will adjust appropriately in cell.
標簽將在單元格中適當調整。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/21/72021058f8d0d52575a03e2441b61821.html。