【发布时间】:2017-12-07 16:08:45
【问题描述】:
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewlayout uicollectionreusableview
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewlayout uicollectionreusableview
您可以使用以下函数找到文本的高度:-
func labelHeight(width:CGFloat , font:UIFont , text:String)->CGFloat{
let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return height:label.frame.height
}
然后将此 UICollectionViewDelegateFlowLayout 方法添加到您的控制器中并返回标题的大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
// Pass parameter into this function according to your requirement
let height = labelHeight(width: collectionView.bounds.width , font:UIFont , text:"")
return CGSize(width:collectionView.bounds.width , height: height + 10)
}
【讨论】: