Ios 动态调整集合视图单元格的大小
我更新了这个较旧的代码示例,但我还是一个新手,不知道如何修复这个错误: 在保存集合视图的类中:Ios 动态调整集合视图单元格的大小,ios,swift,dynamic,uicollectionview,Ios,Swift,Dynamic,Uicollectionview,我更新了这个较旧的代码示例,但我还是一个新手,不知道如何修复这个错误: 在保存集合视图的类中: var array = ["Just testing this out"] 现在在UICollectionViewDelegateFlowLayout扩展中: private func estimateFrameForText(text: String) -> CGRect { //we make the height arbitrarily large so we don't und
var array = ["Just testing this out"]
现在在UICollectionViewDelegateFlowLayout扩展中:
private func estimateFrameForText(text: String) -> CGRect {
//we make the height arbitrarily large so we don't undershoot height in calculation
let height: CGFloat = 1000
let size = CGSize(width: 398, height: height)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.light)]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var height: CGFloat = 295
//we are just measuring height so we add a padding constant to give the label some room to breathe!
var padding: CGFloat = 14
//estimate each cell's height
if let text = array[indexPath.item].text {
height = estimateFrameForText(text).height + padding
}
return CGSize(width: 398, height: height) }
if let text = array[indexPath.item].text
@IBOutlet weak var TextPosted: UILabel!
另外:
private func estimateFrameForText(text: String) -> CGRect {
//we make the height arbitrarily large so we don't undershoot height in calculation
let height: CGFloat = 1000
let size = CGSize(width: 398, height: height)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.light)]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var height: CGFloat = 295
//we are just measuring height so we add a padding constant to give the label some room to breathe!
var padding: CGFloat = 14
//estimate each cell's height
if let text = array[indexPath.item].text {
height = estimateFrameForText(text).height + padding
}
return CGSize(width: 398, height: height) }
if let text = array[indexPath.item].text
@IBOutlet weak var TextPosted: UILabel!
这就是我得到错误的地方:
private func estimateFrameForText(text: String) -> CGRect {
//we make the height arbitrarily large so we don't undershoot height in calculation
let height: CGFloat = 1000
let size = CGSize(width: 398, height: height)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.light)]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var height: CGFloat = 295
//we are just measuring height so we add a padding constant to give the label some room to breathe!
var padding: CGFloat = 14
//estimate each cell's height
if let text = array[indexPath.item].text {
height = estimateFrameForText(text).height + padding
}
return CGSize(width: 398, height: height) }
if let text = array[indexPath.item].text
@IBOutlet weak var TextPosted: UILabel!
这是我不知道在哪里实现的集合视图单元格类的UILabel:
private func estimateFrameForText(text: String) -> CGRect {
//we make the height arbitrarily large so we don't undershoot height in calculation
let height: CGFloat = 1000
let size = CGSize(width: 398, height: height)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.light)]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var height: CGFloat = 295
//we are just measuring height so we add a padding constant to give the label some room to breathe!
var padding: CGFloat = 14
//estimate each cell's height
if let text = array[indexPath.item].text {
height = estimateFrameForText(text).height + padding
}
return CGSize(width: 398, height: height) }
if let text = array[indexPath.item].text
@IBOutlet weak var TextPosted: UILabel!
您的测试阵列需要进行一些调整。尝试将其声明为可选(因为它可能包含从某处加载的实际数据) 那么这个,
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array?.count ?? 0
}
然后更新CollectOnView sizeForItemAtIndexPath函数中的if-let语句:
if let text = array?[indexPath.item]{
}
顺便说一句,几天前我写了一个开源软件来测量像这样的东西的字符串大小
希望这有帮助。您的测试阵列需要一些调整。尝试将其声明为可选(因为它可能包含从某处加载的实际数据) 那么这个,
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array?.count ?? 0
}
然后更新CollectOnView sizeForItemAtIndexPath函数中的if-let语句:
if let text = array?[indexPath.item]{
}
顺便说一句,几天前我写了一个开源软件来测量像这样的东西的字符串大小
希望这能有所帮助。您会遇到什么错误?您希望看到什么?宽度和高度?你得到了什么错误?你期望动态地得到什么?宽度和高度?