gpt4 book ai didi

Swift:动态调整 UICollectionViewCell 大小

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

我试图根据文本的高度调整 UICollectionViewCell 的高度。由于某种奇怪的原因,文本的高度极其困惑。我在帖子here中采用了各种解决方案, herehere但不知何故,所有这些都不适用于我的情况。

我的方法涉及自动布局,我认为这不是一个促成因素。到目前为止我的代码:

class ChatBubbleCollectionViewCell: UICollectionViewCell {

let textView: UITextView = {
let tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.backgroundColor = .lightGray
tv.font = UIFont.systemFont(ofSize: 17)
tv.isScrollEnabled = false
tv.isEditable = false
tv.sizeToFit()
tv.contentInset.top = -4
return tv
}()

let bubbleView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.layer.cornerRadius = 8
return v
}()

var bubbleWidthAnchor: NSLayoutConstraint!
var bubbleLeftAnchor: NSLayoutConstraint!
var bubbleRightAnchor: NSLayoutConstraint!

override init(frame: CGRect) {
super.init(frame: frame)

setupViews()

backgroundColor = .blue
}

func setupViews() {
addSubview(bubbleView)
addSubview(textView)

bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
bubbleView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

let viewWidth = UIScreen.main.bounds.width

bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: viewWidth * 0.8)
bubbleWidthAnchor.isActive = true

bubbleLeftAnchor = bubbleView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8)
bubbleLeftAnchor.isActive = false

bubbleRightAnchor = bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8)
bubbleRightAnchor.isActive = true

textView.topAnchor.constraint(equalTo: bubbleView.topAnchor, constant: 4).isActive = true
textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 4).isActive = true
textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor, constant: -4).isActive = true
textView.bottomAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: -4).isActive = true
}

//At UICollectionViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChatBubbleCollectionViewCell
let item = messages[indexPath.item]

if item.fromUserUID == fromUserUID {
cell.bubbleView.backgroundColor = .customPink
cell.textView.text = item.message
cell.textView.textColor = .white
cell.bubbleLeftAnchor.isActive = false
cell.bubbleRightAnchor.isActive = true
} else {
cell.bubbleView.backgroundColor = .faintGray
cell.textView.text = item.message
cell.textView.textColor = .black
cell.bubbleLeftAnchor.isActive = true
cell.bubbleRightAnchor.isActive = false
}

cell.bubbleWidthAnchor.constant = estimatedFrame(for: item.message!).width

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height: CGFloat = 0

if let text = messages[indexPath.item].message {
height = ceil(estimatedFrame(for: text).height)
}

return CGSize(width: view.frame.width, height: height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}

func estimatedFrame(for text: String) -> CGRect {
let screenWidthDownsize = UIScreen.main.bounds.width * 0.8
let size = CGSize(width: screenWidthDownsize, height: CGFloat.greatestFiniteMagnitude)
let boundingBox = NSString(string: text).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)], context: nil)
return boundingBox
}

上面的代码给出了以下结果:

enter image description here

如果我在高度上添加 50 来展开单元格,就像这样 height = ceil(estimatedFrame(for: text).height) + 50 结果给出:

enter image description here

从图像中,我想人们可以观察到boundingBox并没有真正给出正确的值?接下来我可以尝试什么?

最佳答案

当您在estimatedFrame函数中将宽度设置为80%时,您可能没有考虑UITextView中的填充,并且textview中的可用宽度与80%计算中的可用宽度略有不同,从而导致正在计算不同的高度。

关于Swift:动态调整 UICollectionViewCell 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48971667/

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