реализовать этот делегат UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let market = self.markets[indexPath.row]
let label = UILabel(frame: CGRect.zero)
label.text = market
label.sizeToFit()
return CGSize(width: label.frame.width, height: 25)
}
или
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = NSAttributedString(string: markets[indexPath.row])
return CGSize(width: text.size().width, height: 25)
}