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)
    }

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...