ios - UICollectionViewCell 根据通用应用程序中的文本量垂直调整大小
<p><p>我是一名业余 iOS 开发人员。我在 iOS 中制作了一个简单的单窗口通用应用程序来演示我的问题。在界面生成器中,我将 UICollectionView 添加到我的 View 中,并为其提供了特定的约束以覆盖整个屏幕。然后我制作了一个原型(prototype)单元格,并使用实用程序面板将其宽度设置为 580,高度设置为 80。并在这个单元格中添加了一个标签,并在界面构建器本身中给出了约束。</p>
<p> <a href="/image/08zYg.jpg" rel="noreferrer noopener nofollow"><img src="/image/08zYg.jpg" alt="enter image description here"/></a> </p>
<p>现在,如果我运行这个项目(模拟器设备 iPhone 6),单元格的宽度不会根据设备的屏幕进行调整。它走出屏幕,如下所示:</p>
<p> <a href="/image/lbvqm.png" rel="noreferrer noopener nofollow"><img src="/image/lbvqm.png" alt="enter image description here"/></a> </p>
<pre><code>This thing also gives me following messages in the console while running the project:
2015-11-10 11:11:15.688 uicillectionview auto-sizing the behavior of the UICollectionViewFlowLayout is not defined because:
2015-11-10 11:11:15.689 uicillectionview auto-sizing 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 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 Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
</code></pre>
<p>现在,如果我将以下代码行添加到 <code>ViewController.m</code>,CollectionView 看起来完全正常,不会在控制台中出现任何错误:</p>
<pre><code>- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(.bounds.size.width-20.0, 80);
}
</code></pre>
<p>添加上述代码并运行项目后,根据设备屏幕大小完美适配宽度如下:
<a href="/image/KKf9q.png" rel="noreferrer noopener nofollow"><img src="/image/KKf9q.png" alt="enter image description here"/></a> </p>
<p>我想实现这样的功能,使 CollectionViewCell 根据可变文本的大小调整单元格的高度以完全适应它,并根据设备屏幕宽度调整宽度。有人可以帮忙吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需计算文本的高度。这是方法。</p>
<pre><code>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
}
</code></pre>
<p>那么你可以这样使用</p>
<pre><code>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)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UICollectionViewCell 根据通用应用程序中的文本量垂直调整大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33623899/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33623899/
</a>
</p>
页:
[1]