在UICollectionView中将boundingRectWithSize与UITextView一起使用

时间:2018-09-26 17:09:33

标签: ios objective-c uicollectionview uitextview

大家好,我正在尝试根据UITextView的文本长度来扩展我的collectionView单元格。

为此,我决定实现方法boundingRectWithSize,但是当我运行代码时,控制台始终将高度返回0。

我不明白我哪里错了...因为高度始终为0?

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {

    SlideMenuCollectionViewCell *cell = (SlideMenuCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    CGSize size = CGSizeMake(collectionView.frame.size.width, 1000);
    NSDictionary *attr = @{ NSFontAttributeName : [UIFont systemFontOfSize:12]};
    CGRect extimate = [cell.subtitleCell.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:attr context:nil];

    NSLog(@"%f", extimate.size.height);

    return CGSizeMake(collectionView.frame.size.width, extimate.size.height);
}

我的安慰

2018-09-26 19:00:07.882366+0200[3676:1020208] 0.000000
2018-09-26 19:00:07.882556+0200[3676:1020208] 0.000000
2018-09-26 19:00:07.882657+0200[3676:1020208] 0.000000
2018-09-26 19:00:07.882742+0200[3676:1020208] 0.000000

0 个答案:

没有答案