0

UICollectionViewLayout 서브 클래스를 만들고 PrepareLayout, layoutAttributesForItemAtIndexPath, layoutAttributesForElementsInRect과 같은 필요한 모든 메소드를 구현했습니다. 이전에는 UICollectionViewCell의 높이가 고정되어 있었지만 이제는 요구 사항이 변경됨에 따라 레이블 높이 당 셀 높이가 조정됩니다. UICollectionViewLayout 클래스의 라벨 텍스트를 어떻게 얻을 수 있습니까?내 customCollectionViewLayout에서 Label의 높이를 어떻게 알 수 있습니까?

모든 유형의 힌트 또는 도움을 주시면 감사하겠습니다.

답변

0

나는 VC의 UICollectionViewDelegateFlowLayout 확장에서 cellWidthsizeForItemAtIndexPath을 정의하는데 이것을 사용했다.

장치의 동적 높이 및 너비에 따라 cellWidth으로 정의됩니다.

let cellWidth = UIScreen.mainScreen().bounds.width/7 - 1 

동적 셀 폭 및 높이를 그에 따라 반환하는 함수입니다. 너비와 높이가 같은 치수가되어야하고, 7 행 7 열이 필요한 경우 7로 나누었습니다. 한 줄에 셀이 필요에 따라 변경할 수 있습니다.

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context 

매개 변수 사용 :이있는 NSString 방법을 사용할 수 있습니다 레이블, 텍스트 및 글꼴의 폭을 알고

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: cellWidth, height: cellWidth) 
} 
+0

그러나 'UICollectionViewLayout'이 아니라'UICollectionViewLayout'을 서브 클래 싱했습니다. –

0

size - 폭은의 폭과 동일해야한다 레이블, 높이로 CGFLOAT_MAX를 넣을 수 있습니다.

options -이 라벨

context을 위해 사용할 것이다 글꼴 인 키 NSFontAttributeName과 가치와 사전 - - 단지 NSStringDrawingUsesLineFragmentOrigin

attributes를 사용하면 통과 할 수

반환 값

여기 전무 CGRect가 될 것입니다.이 중 단지 높이를 취하는 것이고 그 안에 텍스트가있는 셀의 레이블 높이가됩니다.