0

Dans mon application Swift j'utilise UICollectionView. Chaque UICollectionViewCell contient un UILabel et à un moment donné, je remplis ces étiquettes avec différentes chaînes de mon tableau. Cependant, les chaînes ont une longueur différente, donc je voudrais les afficher avec une taille correcte. Actuellement, je suis en utilisant ces deux méthodes:
pourquoi je ne peux pas changer la largeur de mon UICollectionViewCell basé sur la longueur d'une étiquette qui est dedans?

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) 
    } 

mais il ne fonctionne pas, les cellules sont toujours les mêmes en taille. Qu'est-ce qui me manque?

+0

-vous montrer votre sortie que vous obtenez et ce que vous voulez réellement. Nous avons donc eu une idée –

Répondre

3

Je pense que vous pouvez changer la largeur de UICollectionViewCell.

Dans la méthode sizeForItemAtIndexPath vous pouvez définir la taille de UICollectionViewCell par le code suivant:

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

Utilisez UICollectionViewDelegateFlowLayout méthode délégués.

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 
    } 

Sortie:

Output