2017-05-19 35 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