OGeek|极客世界-中国程序员成长平台

标题: ios - Collection View 单元格大小取决于 UITextView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:29
标题: ios - Collection View 单元格大小取决于 UITextView

我有一个 UICollectionView 和一个包含 UITextViewCell。我希望单元格的大小取决于 TextView 的高度(因此不需要滚动并且所有文本始终可见)。 我正在尝试这个:

    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)

}

但是它并没有真正起作用。有时高度太大,有时太小,我仍然需要滚动。



Best Answer-推荐答案


使用 TextView 的 sizeThatFits 方法。它精确计算 UITextView 需要的大小。

关于ios - Collection View 单元格大小取决于 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430656/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4