简体   繁体   English

取决于UITextView的集合视图单元格大小

[英]Collection View Cell Size Dependent on UITextView

I have a UICollectionView with a Cell containing a UITextView . 我有一个UICollectionView和一个包含UITextViewCell I want the size of the cell to depend on the height of the TextView (so that scrolling isn't necessary and all the text is always visible). 我希望单元格的大小取决于TextView的高度(以便不需要滚动,并且所有文本始终可见)。 I am trying this: 我正在尝试:

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    if (indexPath.row < myComments!.count) {

        if let messageText = myComments![indexPath.row].text  {
            let size = CGSizeMake(self.view.frame.width / 2, 100)
            let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
            let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil)
            return CGSizeMake(view.frame.width, estimatedFrame.height + 50)
        }
    }
    return CGSizeMake(view.frame.width, 100)

}

however it isn't really working. 但是它不是真的工作。 Sometimes the height is way too big, sometimes its too small and I still have to scroll. 有时高度太大,有时高度太小,我仍然需要滚动。

Use TextView's sizeThatFits method. 使用TextView的sizeThatFits方法。 It calculate exactly size the UITextView need. 它精确计算出UITextView所需的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM