ios – 基于UILabel的动态UICollectionView标题大小

前端之家收集整理的这篇文章主要介绍了ios – 基于UILabel的动态UICollectionView标题大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经读了一堆帖子来添加标题到UICollectionView.在 Swift的iOS 7应用程序中,我试图添加一个带有UILabel的标头,其高度应根据UILabel的高度进行调整. UILabel有线= 0.

我已经在IB中使用AutoLayout设置了标题

ViewController实现UICollectionViewDelegate,UICollectionViewDataSource.我没有为标题设置一个自定义类,但是使用这两个函数

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize {
      //description is a String variable defined in the class
    let size:CGSize = (description as NSString).boundingRectWithSize(CGSizeMake(CGRectGetWidth(collectionView.bounds) - 20.0,180.0),options: NSStringDrawingOptions.UsesLineFragmentOrigin,attributes: [NSFontAttributeName: UIFont(name: "Helvetica Neue",size: 16.0)],context: nil).size
    return CGSizeMake(CGRectGetWidth(collectionView.bounds),ceil(size.height))
}

func collectionView(collectionView: UICollectionView!,viewForSupplementaryElementOfKind kind: String!,atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {
    var reusableview:UICollectionReusableView = UICollectionReusableView()
    if (kind == UICollectionElementKindSectionHeader) {
                    //listCollectionView is an @IBOutlet UICollectionView defined at class level,using collectionView crashes
            reusableview = listCollectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader,withReuseIdentifier: "ListHeader",forIndexPath: indexPath) as UICollectionReusableView
            let label = reusableview.viewWithTag(200) as UILabel  //the UILabel within the header is tagged with 200
            label.text = description   //description is a String variable defined in the class
        }
    }
    return reusableview
}

文本的显示似乎正在工作,但高度计算似乎不起作用(见下面的屏幕截图).此外,我不认为我可以通过collectionView … referenceSizeForHeaderInSection函数访问UILabel.关于如何正确计算CGSize的任何建议?

解决方法

这是我怎么做到的
let labels = [
"Lorem ipsum dolor sit amet,consectetur adipiscing elit. Nunc ac lorem enim. Curabitur rhoncus efficitur quam,et pretium ipsum. Nam eu magna at velit sollicitudin fringilla nec nec nisi. Quisque nec enim et ipsum feugiat pretium. Vestibulum hendrerit arcu ut ipsum gravida,ut tincidunt justo pellentesque. Etiam lacus ligula,aliquet at lorem vel,ullamcorper commodo turpis. Nullam commodo sollicitudin mauris eu faucibus.","Lorem ipsum dolor","Lorem ipsum dolor sit amet,et pretium ipsum. Nam eu magna at velit sollicitudin fringilla nec nec nisi. Quisque nec enim et ipsum feugiat pretium."]

基本思想是创建一个相同的UILabel来计算它的大小,以帮助我们在ReferenceSizeForHeaderInSection方法中.

我的UICollectionReusableView子类(MyHeaderCollectionReusableView)中有一个名为“label”的标签出口,我通过在故事板中分配我的部分标题视图(将“MyHeader”设置为部分视图的重用标识符).所提到的标签对于段标题边界具有水平和垂直空间限制,以便正确地自动布局.

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 3
    }

override func collectionView(collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let headerView =
        collectionView.dequeueReusableSupplementaryViewOfKind(kind,withReuseIdentifier: "MyHeader",forIndexPath: indexPath)
            as MyHeaderCollectionReusableView

        headerView.label.text = labels[indexPath.section]

        return headerView

    }

func collectionView(collectionView: UICollectionView,referenceSizeForHeaderInSection section: Int) -> CGSize {
        // that -16 is because I have 8px for left and right spacing constraints for the label.
        let label:UILabel = UILabel(frame: CGRectMake(0,collectionView.frame.width - 16,CGFloat.max))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
       //here,be sure you set the font type and size that matches the one set in the storyboard label
        label.font = UIFont(name: "Helvetica",size: 17.0)
        label.text = questions[section]
        label.sizeToFit()

// Set some extra pixels for height due to the margins of the header section.  
//This value should be the sum of the vertical spacing you set in the autolayout constraints for the label. + 16 worked for me as I have 8px for top and bottom constraints.
        return CGSize(width: collectionView.frame.width,height: label.frame.height + 16)
    }
原文链接:https://www.f2er.com/iOS/336509.html

猜你在找的iOS相关文章