自定义 UiCollectionViewCell class init 未被调用
Custom UiCollectionViewCell class init not being called
您好,我正在使用此代码尝试调整文本大小,但我看不到要调整标签的大小。此外,未调用自定义 class 中的 init 方法。这是我的代码:
class Cell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
print("init—>Not being called???\n")
self.label.adjustsFontSizeToFitWidth = true
self.cell.label.sizeToFit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我该怎么做才能让 init 调用它。我也试过调整方法中的文本
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
虽然标签的文字大小没有变化,
这是带有 longs 字符串的单元格的样子。
预先感谢您的帮助。
如果我放置 -
adjustsFontSizeToFitWidth
或
sizeToFit
:
进入 ->
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.label.adjustsFontSizeToFitWidth = true // causes error
self.label.sizeToFit()
}
我也遇到错误。
将您的代码或断点放在 init?(coder aDecoder: NSCoder)
中。如果您在情节提要中添加了 CollectionView,应该会调用它。
更新
如此处理您的 IBOutlets(在您的单元格子类中),因为在 init 函数中它们可能还无效,我假设是您的情况:
override func layoutSubviews()
{
super.layoutSubviews()
self.label.adjustsFontSizeToFitWidth = true
self.label.minimumScaleFactor = 0.8 //set the scale factor or minimumFontSize so that it can then start adjusting the font size.
Self.label.numberOfLines = 1
}
您好,我正在使用此代码尝试调整文本大小,但我看不到要调整标签的大小。此外,未调用自定义 class 中的 init 方法。这是我的代码:
class Cell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
print("init—>Not being called???\n")
self.label.adjustsFontSizeToFitWidth = true
self.cell.label.sizeToFit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我该怎么做才能让 init 调用它。我也试过调整方法中的文本
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
虽然标签的文字大小没有变化,
这是带有 longs 字符串的单元格的样子。
如果我放置 -
adjustsFontSizeToFitWidth
或
sizeToFit
:
进入 ->
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.label.adjustsFontSizeToFitWidth = true // causes error
self.label.sizeToFit()
}
将您的代码或断点放在 init?(coder aDecoder: NSCoder)
中。如果您在情节提要中添加了 CollectionView,应该会调用它。
更新 如此处理您的 IBOutlets(在您的单元格子类中),因为在 init 函数中它们可能还无效,我假设是您的情况:
override func layoutSubviews()
{
super.layoutSubviews()
self.label.adjustsFontSizeToFitWidth = true
self.label.minimumScaleFactor = 0.8 //set the scale factor or minimumFontSize so that it can then start adjusting the font size.
Self.label.numberOfLines = 1
}