根据标签大小调整UICollectionViewCell的大小

时间:2015-02-09 12:06:18

标签: ios objective-c iphone uicollectionview

我有一个UICollectionView,其中包含不同大小标签的单元格。我正在尝试根据标签的大小调整单元格的大小。但是我创建单元格的sizeForItemAtIndexPath似乎是在cellForItemAtIndexPath之前调用的,我设置了标签..有什么想法我可以在这做什么?

- (void)getLabelSize:(UILabel *)label {
    float widthIs = [label.text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:label.font } context:nil].size.width;

    width = widthIs;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // Configure the cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:1000];
    label.text = @"This is pretty long";
    [label sizeToFit];
    [self getLabelSize:label];
    NSLog([NSString stringWithFormat:@"%f", width]);
    cell.backgroundColor = [UIColor whiteColor];

    return cell;   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(width, 50);
}

4 个答案:

答案 0 :(得分:13)

swift 中,我计算了标签的文字大小,并根据它更新了单元格大小:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {


        let size: CGSize = keywordArray[indexPath.row].size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])
        return CGSize(width: size.width + 45.0, height: keywordsCollectionView.bounds.size.height)
    }

因为我有一个额外的按钮和填充我计算了额外的值45.0

答案 1 :(得分:2)

你需要在某个地方存储标签的文本,例如,在数组中,并单独定义标签的字体并定义maxWidth,如:

#define kLabelFont [UIFont fontWithName:@"SourceSansPro-Regular" size:12.0f]
#define maxWidth 100.0f

然后修改方法getLabelSize,将字符串作为参数:

- (CGSize)getLabelSize:(NSString *)string {
     CGRect rect = [string boundingRectWithSize:(CGSize){maxWidth, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: kLabelFont } context:nil];
     return rect.size;
}

然后你可以从两个方法获得大小:cellForItemAtIndexPath和sizeForItemAtIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ........
     NSString *string = [self.textArray objectAtIndex:indexPath.row];
     CGSize size = [self getLabelSize:string];
    ........
}

答案 2 :(得分:1)

Swift 4.2更新答案的解决方案是在uilabel文本的基础上处理uicollectionview Cell的高度和宽度

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
  let size = (self.FILTERTitles[indexPath.row] as NSString).size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])
    return CGSize(width: size.width + 38.0, height: size.height + 25.0)


}

答案 3 :(得分:0)

首先,您必须将消息存储在临时标签中(确保其隐藏或不可见)。然后得到如下标签的宽度和高度:

templable.test = @"asdasdhasjdhajkdh";

templable.numberOfLines = 0;

[templable sizeToFit];

float width = 0;

width = self.templable.bounds.size.width+20;