2

I am adding labels as subview to UIstackview using arrangedSubView property.

what happens is that if I keep Stackview axis horizontal, I get one horizontal strip of UILabels

label1 label2 label3 label4.....

if I keep it vertical, I get a vertical strip of UILabels like below

label1
label2
label3
and so on

But what I am trying to achieve is this

label1, label2(if it fits) otherwise take it below
label3, label4
label5 and so on

presently my code is this

 uiLabel1.numberOfLines = 1
            uiLabel1.backgroundColor = THEMECOLOR
            uiLabel1.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)
            uiLabel1.textColor = topicColor
            uiLabel1.layer.cornerRadius = (CGFloat)(5.0)
            uiLabel1.layer.masksToBounds = true
            uiLabel1.sizeToFit()
            uiLabel1.userInteractionEnabled = true           


            self.dynamicStackView.addArrangedSubview(uiLabel1)
Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • 1
    This sounds like a UICollectionView might be more what you are looking for. – Maple Jun 27 '16 at 18:29
  • I agree with Maple. Sounds like a job for a collection view. Otherwise you'll need to write code that builds a stack view of stack views dynamically, which would be a fair amount of work. – Duncan C Jun 28 '16 at 00:58

1 Answers1

1

I believe Flow Layout of the UICollectionView is what you need. Have a look at the link here

Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50