ios - UICollectionViewCell 根据通用应用程序中的文本量自动调整垂直大小

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我是一名业余 iOS 开发者。我在 iOS 中制作了一个简单的单窗口通用应用程序来演示我的问题。在界面生成器中,我将 UICollectionView 添加到我的 View 中,并为其指定了覆盖整个屏幕的特定约束。然后我制作了一个原型(prototype)单元,并使用实用程序面板将其宽度设置为 580,高度设置为 80。并向该单元添加了一个标签,并在界面生成器本身中给出了约束。

enter image description here

现在,如果我运行这个项目(模拟器设备iPhone 6),单元格的宽度不会根据设备的屏幕进行调整。它超出屏幕并显示如下:

enter image description here

This thing also gives me following messages in the console while running the project:

2015-11-10 11:11:15.688 uicillectionview auto-sizing[3338:262762] the behavior of the UICollectionViewFlowLayout is not defined because:
2015-11-10 11:11:15.689 uicillectionview auto-sizing[3338:262762] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2015-11-10 11:11:15.689 uicillectionview auto-sizing[3338:262762] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7f981176f810>, and it is attached to <UICollectionView: 0x7f9811862400; frame = (0 20; 375 647); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7f981176e860>; layer = <CALayer: 0x7f9811586a60>; contentOffset: {0, 0}; contentSize: {600, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7f981176f810>.
2015-11-10 11:11:15.689 uicillectionview auto-sizing[3338:262762] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

现在,如果我将以下代码行添加到 ViewController.m 中,CollectionView 将显示完全正常,不会在控制台中给出任何错误:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake([UIScreen mainScreen].bounds.size.width-20.0, 80);

}

添加上述代码并运行项目后,宽度根据设备屏幕尺寸完美适配,如下: enter image description here

我想实现功能,以便 CollectionViewCell 根据可变文本的大小调整单元格的高度以完全适应它,并根据设备屏幕宽度调整宽度。有人可以帮忙吗?

最佳答案

只需计算文本的高度即可。方法如下。

func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
        let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label.font = font
        label.text = text
        label.sizeToFit()
        return label.frame.height
    }

然后你就可以这样使用

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

        let cellWidth = self.view.frame.size.width
        let cellHeight = heightForView("Your Text", font: UIFont.systemFontOfSize(17), width: cellWidth)

        return CGSize(width: cellWidth, height: cellHeight)
    }

关于ios - UICollectionViewCell 根据通用应用程序中的文本量自动调整垂直大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623899/

相关文章:

ios - 捕获 UIView 并另存为图像

ios - UICollectionView 在 reloadData 后不调用 didDeselectItemAtIndexPath

ios - 使用一个 Collection View IOS 滚动多个 UICollectionView

ios - 在 TableView 导航 Controller 之前添加 swift View Controller 的推荐结构

ios - 实例将立即被释放,因为属性 'imageView' 是 'weak'

objective-c - 两个 NSDate 之间的月数和天数

objective-c - 圆弧 : Memory does not get reclaimed?

objective-c - 使用 NSNumberFormatter 在 NSTextField 中自定义错误消息

iphone - 将 UIView 置于所有其他 View 之上

ios - UICollectionView 作为 subview 不更新约束