我有一个使用多个collectionviewcell进行布局的布局。目前,我使用的是静态高度,但众所周知,在我的情况下,内容的大小并不确定,因为文本将占据单元格的大部分。我无法找到一种基于单元格中的textView大小以及填充来精确调整单元格大小的方法。
因此,简而言之,我想问的是,是否有必要根据collectionVIew中事物的大小来调整内容的大小。
这是我目前在牢房中拥有的东西
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)
}
任何帮助将不胜感激
答案 0 :(得分: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委托
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)
}