내 코드에서 머리글과 바닥 글이있는 UICollectonView를 만들었습니다.
내 바닥 글 높이는 텍스트 크기에 따라 동적이어야합니다.
텍스트는 1 줄에서 100 줄까지 가능합니다.
다음은 내 코드입니다.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width:collectionView.frame.size.width, height:50.0)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionFooter:
let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooterCollectionReusableView", for: indexPath) as! SectionFooterCollectionReusableView
footer.lblDescription.text = composition?.desc
return footer
default: return UICollectionReusableView()
}
}
문자열 확장에 기능을 추가 할 수 있습니다.
extension String {
func heightWithConstrainedWidth(_ width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
}
그런 다음 귀하의 방법에서 텍스트 높이를 계산하십시오.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if let string = composition?.desc {
let height = string.heightWithConstrainedWidth(width, font: font)
//calculate needed height
return CGSize(width:collectionView.frame.size.width, height:neededHeight)
}
return CGSize(width:collectionView.frame.size.width, height:50.0)
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다