0

私の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つのメソッドを使用しています。 私は何が欠けていますか?

+0

はあなたが達成あなたの出力を示してください、あなたが実際に何をしたいです。だから私たちは考えを得た –

答えて

3

UICollectionViewCellの幅を変更できると思います。

sizeForItemAtIndexPath方法では、次のコードでUICollectionViewCellのサイズを設定することができます。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
{ 
    var size = CGSizeZero 
    if let text = self.suggestedHashtags[indexPath.item] as? String { 
     size = text.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18.0)]) 
     return size 
    } 
    return size 
} 
2

使用UICollectionViewDelegateFlowLayout委譲する方法を。

class CONTROLLERNAME: UIViewController ,UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{ 

var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"]; 


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     return CGSize(width: self.findHeightWidthForText(self.items[indexPath.item], havingWidth: self.view.frame.size.width - 18, andFont: UIFont.systemFontOfSize(17.0)).width + 12, height: 36) // here pass your font size and ya give extra some space for cell. 
} 


// method for find hight and width for particular string. 
func findHeightWidthForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize { 
     var size = CGSizeZero 
     if text.isEmpty == false { 
      let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) 
      size = CGSizeMake(frame.size.width, ceil(frame.size.height)) 
     } 
     return size 
    } 

出力

Output

関連する問題