3

예제 프로젝트의 CollectionViewLayout을 https://github.com/hebertialmeida/HAPaperViewControllerhttps://github.com/wtmoose/TLLayoutTransitioning으로 전환하면서 놀고 있는데, 작은 크기와 작은 크기 사이의 셀 내용을 자동으로 조정하는 방법을 알지 못했습니다. 큰 레이아웃.레이아웃 전환 내에서 UICollectionView 셀의 크기 조절/크기 조절

예. CollectionViewCell 내에 정의 된 (고정 된) 위치에서 셀 너비의 절반이어야하는 UILabel이 있습니다. 큰 레이아웃으로의 전환이 완료되면 레이블은 셀의 절반 (동일한 상대 위치)이지만 글꼴 크기가 커지거나 크기가 조정되어야합니다.

여기에서 autolayout을 사용하거나 CGAffineTransformMakeScale을 사용하여 contentView의 배율을 조정 하시겠습니까?

+0

죄송합니다. 나는 GitGHub에 답변과 샘플 코드를 게시했다. –

답변

3

이 작업을 수행하는 방법을 보여주기 위해의 "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; 
}]; 
관련 문제