gpt4 book ai didi

ios - 动态大小 CollectionViewCells

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

我有一个布局,它使用多个collectionviewcells来布局。目前我使用的是静态高度,但众所周知,无法确定内容的大小,特别是在我的情况下,因为它将是占据单元格大部分的文本。我无法找到一种准确的方法来根据单元格中 textView 的大小以及填充来调整单元格的大小。

所以简而言之,我想问的是,是否可以根据 Collection View 中内容的大小来调整内容的大小。

这就是我目前的单元格

import UIKit

class DetailCell: UICollectionViewCell {

var eventDescription: String? {
didSet{
print("got description")
guard let eventDescription = eventDescription else {
return
}
eventDetails.text = eventDescription
updateWithSpacing(lineSpacing: 10.0)
textViewDidChange(eventDetails)

}
}




lazy var eventDetails: UITextView = {
let textView = UITextView()
guard let customFont = UIFont(name: "NoirPro-SemiBold", size: 15) else {
fatalError("""
Failed to load the "CustomFont-Light" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
textView.font = customFont
textView.textColor = UIColor.rgb(red: 32, green: 32, blue: 32)
textView.textContainer.maximumNumberOfLines = 0
textView.isScrollEnabled = false
textView.isEditable = false
textView.textAlignment = .natural
return textView
}()


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

fileprivate func setupViews(){
addSubview(eventDetails)
eventDetails.delegate = self

eventDetails.snp.makeConstraints {
make in
make.top.equalTo(detailsLabel.snp.bottom).offset(15)
make.left.right.equalTo(self).inset(5)
make.height.equalTo(50)
}

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


//MARK: - Update Line Spacing
func updateWithSpacing(lineSpacing: Float) {
// The attributed string to which the
// paragraph line spacing style will be applied.
let attributedString = NSMutableAttributedString(string: eventDetails.text!)
let mutableParagraphStyle = NSMutableParagraphStyle()
// Customize the line spacing for paragraph.
mutableParagraphStyle.lineSpacing = CGFloat(lineSpacing)
mutableParagraphStyle.alignment = .justified
if let stringLength = eventDetails.text?.count {
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: mutableParagraphStyle, range: NSMakeRange(0, stringLength))
}
// textLabel is the UILabel subclass
// which shows the custom text on the screen
eventDetails.attributedText = attributedString

}

}

extension DetailCell: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: self.frame.width - 5, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}

}

这是我的电池尺寸。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 350.0)
}

如有任何帮助,我们将不胜感激

最佳答案

您可以在移动时找出单元格的高度

extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont?) -> CGFloat {
guard let fontValue = font else {
return 0
}
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect,
options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName: fontValue],
context: nil)
if boundingBox.height >= 80 {
return 80
}
return ceil(boundingBox.height)
}
}

之后,您可以使用此字符串扩展来获取高度值并将其传递给 collectionView delegate

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let feature = cellData.arrayOfData[indexPath.row]
let height = feature.height(withConstrainedWidth: collectionView.frame.width,
font:"FontYouAddedToTextView")
return CGSize(width: collectionView.frame.width, height: height)
}

关于ios - 动态大小 CollectionViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52489316/

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