gpt4 book ai didi

ios - CollectionView Label 多行标签自动布局在小屏幕 iOS 设备上陷入困境

转载 作者:行者123 更新时间:2023-11-30 11:39:14 25 4
gpt4 key购买 nike

CollectionViewCell 中有两个标签(labelLeftlabelRight)。以下代码确保多行标签增长而无需计算标签的高度。该代码在较大的屏幕(iPhone 7 Plus 等)上运行得非常好,但在 iPhone 5S 上该代码就会陷入困境。它会在应用程序挂起时不断寻找 collectionView 属性。我尝试减小宽度,但到目前为止没有成功,可能是因为我不明白 estimatedItemSize 的工作原理。任何帮助将不胜感激。

let dummyArrayRight: [String] = ["dummy text left left left left left left LEFT", "dummy text left LEFT"]
let dummyArrayLeft: [String] = ["dummy text RIGHT RIGHT RIGHT RIGHT", "dummy text right right right right right right right right right RIGHT"]

TableViewCell:

if let flowLayout = cell.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
}

CollectionViewCell:

cell.labelLeft.attributedText = self.dummyArrayLeft[indexPath.row].html2AttributedString
cell.labelRight.attributedText = self.dummyArrayRight[indexPath.row].html2AttributedString

let widthSize = UIScreen.main.bounds.width / 2
cell.labelLeft.preferredMaxLayoutWidth = widthSize
cell.labelRight.preferredMaxLayoutWidth = widthSize
cell.collectionLabel.textAlignment = .left
cell.labelRight.textAlignment = .right

enter image description here

最佳答案

使用以下委托(delegate)方法:UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: (self.view.frame.width - 8) / 2.0 , height: (self.view.frame.width - 8) / 2.0)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

return UIEdgeInsetsMake(8, 8, 0, 8)
}

This是它的原始答案。

关于ios - CollectionView Label 多行标签自动布局在小屏幕 iOS 设备上陷入困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49427001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com