我正在尝试从https://github.com/hebertialmeida/HAPaperViewController和https://github.com/wtmoose/TLLayoutTransitioning转换示例项目的CollectionViewLayout,我不知道如何在小布局和大布局之间自动调整单元格内容的大小。
例如,我在CollectionViewCell中有一个UILabel,它应该是定义(固定)位置的单元格宽度的一半。当完成到大布局的转换时,标签也应该是单元格的一半(相同的相对位置),但具有更大的字体大小(或调整大小)。
在这里使用自动布局还是使用CGAffineTransformMakeScale缩放contentView?
发布于 2014-04-09 05:20:57
我已经更新了TLLayoutTransitioning中的"Resize“示例项目,以演示如何做到这一点。
该方法涉及使用TLLayoutTransitioning回调之一在转换的每个步骤更新字体大小。你可能不想使用仿射变换,因为你会得到大尺寸的模糊缩放文本。
第一步是定义一个方法来设置标签的字体大小。你可以使用你喜欢的任何公式,但我已经使字体与单元格的宽度成比例缩放:
- (void)updateLabelScale:(UILabel *)label cellSize:(CGSize)cellSize
{
CGFloat pointSize = cellSize.width * 17 / 128.f;
label.font = [UIFont fontWithName:label.font.fontName size:pointSize];
}
您需要在cellForItemAtIndexPath
中调用此方法,以确保屏幕上显示的新标签被正确缩放:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
...
[self updateLabelScale:label cellSize:cell.bounds.size];
return cell;
}
标签的中心被约束到单元格的中心,大小由intrinsicContentSize
决定。因此,当您更改字体时,标签的大小将自动调整到合适的大小。
最后,您将使用updateLayoutAttributes
回调根据新的布局属性更新可见单元格的字体大小(您无需担心不可见的单元格,因为您将在cellForRowAtIndexPath
中处理这些单元格):
__weak ResizeCollectionViewController *weakSelf = self;
[layout setUpdateLayoutAttributes:^UICollectionViewLayoutAttributes *(UICollectionViewLayoutAttributes *pose, UICollectionViewLayoutAttributes *fromPose, UICollectionViewLayoutAttributes *toPose, CGFloat progress) {
CGSize cellSize = pose.bounds.size;
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:pose.indexPath];
if (cell) {
UILabel *label = (UILabel *)[cell viewWithTag:1];
[weakSelf updateLabelScale:label cellSize:cellSize];
}
return nil;
}];
https://stackoverflow.com/questions/22627637
复制相似问题