下面的代码可以正常工作,但是当涉及到较长的文本时,它只是根据视图框架的宽度来剪切一些文本显示,例如replacing it with abstractions or using data normalization to avoid redundancy.
replacing it with abstractions or using data n
显示其他未显示的内容
class AyatWordByWordViewController : UIViewController {
let celled = "celled"
var texts : [String] = ["Don't", "repeat", "yourself", "is a principle", "of", "software development", "aimed", "at", "reducing", "repetition of software patterns", "replacing it with abstractions or using data normalization to avoid redundancy."]
//word by work
var collectionView : UICollectionView!
override func viewDidLoad(){
let alignedFlowLayout = UICollectionViewFlowLayout()
alignedFlowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
collectionView = UICollectionView(frame: .zero, collectionViewLayout: alignedFlowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(AyatTableViewWordByWordItemCell.self, forCellWithReuseIdentifier: celled)
collectionView.backgroundColor = UIColor.clear
self.view.addSubview(collectionView)
collectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 150, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 0)
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
// flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
}
}
}
extension AyatWordByWordViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return texts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:celled, for: indexPath) as! AyatTableViewWordByWordItemCell
cell.subTitleLabel.text = texts[indexPath.item]
cell.backgroundColor = UIColor.blue
return cell
}
}
单元格:
public func fillSuperview() {
translatesAutoresizingMaskIntoConstraints = false
if let superview = superview {
leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
}
}
已使用:
subTitleLabel.fillSuperview()
答案 0 :(得分:0)
请在code
下尝试
let layout = yourCollectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = UICollectionViewFlowLayout.automaticSize
layout.estimatedItemSize = CGSize(width: 93, height: 31)
layout.sectionInset = UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 0)
为标签提供单元格的前导,尾随,顶部和底部约束,希望它对您有用