2017-05-19 38 views
0

我有來自服務器的單詞,並且動態地設置了集合視圖,每個單元顯示一個字母。UICollectionview動態適合一行

但如果這個詞太長,那麼一切都縮水。

enter image description here enter image description here

我想每個字母以適應集合視圖,這個詞應該適合在一行而已,所以我用:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let numberOfSets = CGFloat(self.letters!.count) 
    let width = (collectionView.frame.size.width - (numberOfSets * view.frame.size.width/15))/numberOfSets 
    let height = collectionView.frame.size.height/2 
    return CGSize(width : width, height : height) 
} 
+1

你必須如果您不希望它們縮小,請保持collectionViewCell的寬度不變。 –

+0

@SanketBhavsar我希望他們縮小,但比例..如何可以保持不變?他們不會合併成一行 – knic1ned

+0

文字是否縮小可以嗎? –

回答

0

選擇UILabel並轉到屬性檢查器

Attribute Inspector Snap

然後se lect 最小字體大小最小字體比例來自Autoshrink選項,最後設置您所需的最小值值。

希望這個工程。

+0

我已經在標籤 – knic1ned

+0

上自動縮小,您已將最小字體大小設置爲? –

+0

是的,http://i.imgur.com/2g9maKv.png – knic1ned

0

我認爲你需要的是保持細胞左對齊,以及固定的細胞寬度和細胞之間的填充。當字母很少時不是動態填充。你不希望「a b c」變成「a ........ b ........ c」。解決方法是在單元格之間設置一個最大填充,不幸的是,默認的集合視圖佈局沒有這種設置。您需要定義一個:

class MaxSpacingCollectionViewFlowLayout : UICollectionViewFlowLayout { 
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     guard let attributes = super.layoutAttributesForElements(in: rect) else { 
      return nil 
     } 

     if attributes.count <= 0 { return attributes } 

     let firstCellOriginX = attributes[0].frame.origin.x 

     for i in 1..<attributes.count { 
      let currentLayoutAttributes = attributes[i]; 
      if currentLayoutAttributes.frame.origin.x == firstCellOriginX { 
       continue //first cell of a new row 
      } 

      let prevLayoutAttributes = attributes[i - 1] 
      let prevOriginMaxX = prevLayoutAttributes.frame.maxX 
      if currentLayoutAttributes.frame.origin.x - prevOriginMaxX > 8 { // 8 here is the max padding 
       var frame = currentLayoutAttributes.frame 
       frame.origin.x = prevOriginMaxX + 8 
       currentLayoutAttributes.frame = frame 
      } 
     } 

     return attributes 
    } 
} 

然後替換默認佈局:

let collectionView = UICollectionView() 
    //...... 
    collectionView.collectionViewLayout = MaxSpacingCollectionViewFlowLayout() 

最後,委託方法sizeForItemAt可以簡單地返回一個固定的寬度:

return CGSize(width : 35, height : 35) 
+0

謝謝!但是當文本太長時線條會分裂:http://i.imgur.com/03P4ACd.png – knic1ned

+0

爲什麼不給collectionView更多的寬度?這是有限的? –

0

試試這個:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

let size: CGSize = self.letters[indexPath.row].size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18.0)])//18.0 replace it with your label font size. 

let height = collectionView.frame.size.height/2 

return CGSize(width: size.width+2 , height: height) 

} 
+0

'.letters [indexPath.row] .size'是什麼意思?獲取標籤字體大小? – knic1ned

+0

'let size:CGSize = self。字母[indexPath.row] .size(屬性:[NSFontAttributeName:UIFont.systemFont(ofSize:YOUR_Label_Font_Size)])'將返回您的內容所需的確切大小。 – Maddy

+0

我已經測試了上面的代碼,它爲我工作至少。 – Maddy