私のSwift
では、私はUICollectionView
を使っています。各UICollectionViewCell
にはUILabel
が含まれています。ある時点で、それらのラベルに配列とは異なる文字列が設定されています。しかし、文字列の長さが異なるので、正しいサイズで表示したいと思います。細胞はまだサイズが同じである、
なぜ内部のラベルの長さに基づいてUICollectionViewCellの幅を変更できないのですか?
private func estimateFrameForText(text: String) -> CGRect {
//we make the width arbitrarily large so we don't undershoot height in calculation
let width: CGFloat = 200
let size = CGSize(width: width, height: 33)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSFontAttributeName: font]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var width: CGFloat = 200
//we are just measuring width so we add a padding constant to give the label some room to breathe!
var padding: CGFloat = 1
//estimate each cell's width
if let text = self.suggestedHashtags[indexPath.item] as? String {
width = estimateFrameForText(text: text).width + padding
}
return CGSize(width: width, height: 35)
}
をしかし、それは動作しません:現在、私は、これらの2つのメソッドを使用しています。 私は何が欠けていますか?
はあなたが達成あなたの出力を示してください、あなたが実際に何をしたいです。だから私たちは考えを得た –