Sup*_*off 6 objective-c width ios uicollectionview cgsize
我有一个iOS应用,带有一个UICollectionView
可水平显示单元格的应用。在每个单元格中,我都添加了一个简单的标签(目前)。这些标签显示的是名称,有时名称很短,没关系...但是....有时名称很长,因此被切除,因为它们不能正确地容纳在集合视图单元格的宽度内。
有没有一种方法可以动态调整单元格/标签的宽度,以便正确显示名称文本?
我正在尝试使用这种方法:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(106.f, 60.f);
}
Run Code Online (Sandbox Code Playgroud)
但是上述方法存在两个主要问题:
谢谢你的时间,
担
感谢大家的发帖,最终对我有用的解决方案是所有解决方案的混合和一些额外的“修补”:
-(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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1637 次 |
最近记录: |