感谢大家发帖,最后对我有用的解决方案是你所有的解决方案和一些额外的“修补”的混合:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the user name and profile name strings.
NSString *user_label = [NSString stringWithFormat:@"%@", [user_data[indexPath.row] objectAtIndex:0]];
NSString *name_label = [NSString stringWithFormat:@"%@", [user_data[indexPath.row] objectAtIndex:1]];
// Calculate the contact text widths.
NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"SFUIDisplay-Thin" size:20.0f]};
CGRect rect = [user_label boundingRectWithSize:CGSizeMake(MAXFLOAT, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
CGRect rect_two = [name_label boundingRectWithSize:CGSizeMake(MAXFLOAT, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
// Add the other cell objects width to
// the biggest calculated text width.
CGFloat row_width = 0;
if (rect.size.width > rect_two.size.width) {
row_width = (rect.size.width + 55);
}
else {
row_width = (rect_two.size.width + 55);
}
// Only return the generated width if
// it is bigger than the original width.
if (row_width > 106) {
return CGSizeMake(row_width, 60.f);
}
else {
return CGSizeMake(106, 60.f);
}
}