1

I need a dynamic height label based on text length. I have set lines = 0, and Line break = word wrap.

uilabel setting

But its not working

set text

result

Is there any setting that I've missed? Seems like the default behavior is not wrapping, but i have changed it to "word wrap" but the label height is still not expanding.

zero3nna
  • 2,770
  • 30
  • 28
dann
  • 853
  • 5
  • 17
  • 2
    Can you please show all the constraints given? – Sanket_B Jul 23 '18 at 12:25
  • 1
    If you added the height constraints then please remove it – Sarabjit Singh Jul 23 '18 at 12:25
  • No i don't set height constraint. only top, leading, and trailing to safe area. and second label top constraint to first label, leading and trailing to safe area. – dann Jul 23 '18 at 12:27
  • 1
    Very well asked, with amazingly complete info, especially since it’s so hard to show storyboard settings clearly. You really worked hard to prepare this question! Much appreciated. But given what you’ve shown, the height should definitely grow. Could you post the project on github or similar? I’ll be happy to take a look at it. – matt Jul 23 '18 at 12:30
  • 2
    And what are the constraints of second label? – shivi_shub Jul 23 '18 at 12:34
  • set Line break = truncate tail, and decrease the first label's content vertical hugging priority less than label2 and check or do opposite also and check if it works. – vivekDas Jul 23 '18 at 12:39
  • [Is this](https://stackoverflow.com/questions/36554130/autolayout-to-dynamically-size-uilabel-height-and-width) related to what are you looking for? – Ahmad F Jul 23 '18 at 12:40
  • Remove second label constraints and then run and test – Sarabjit Singh Jul 23 '18 at 12:47
  • Please don't post code as an image - it's not searchable – Ashley Mills Jul 23 '18 at 13:07
  • Thanks all, i've found the answer. The code in this answer has caused this : https://stackoverflow.com/a/44145859/1799096 – dann Jul 23 '18 at 13:14

1 Answers1

3

I did everything you did:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.label1.text = "adsfadsf adf adfasdf adfadf adsfadf adfafasdfa adfadf adfadff adsfadfasdf adsfadfasf"
}

enter image description here

enter image description here

And this is what I got:

enter image description here

That's what you should get too. If you aren't, something else is going on.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Suggestion: use the View Debugger to see if the label boundaries are where they should be. Maybe the label is stretching out to the right somehow. – matt Jul 23 '18 at 12:56
  • you are correct, something else is causing this.. I searched for UILabel extension in the project and found a code like like in this answer : https://stackoverflow.com/a/44145859/1799096 – dann Jul 23 '18 at 13:11
  • i deleted the extension, and the UI label is back to normal, thanks – dann Jul 23 '18 at 13:12
  • I continued this project from someone, so little bit confused why it's not working – dann Jul 23 '18 at 13:13
  • Sometimes deleting everything _except_ the code that should be working shows you that that code does indeed work, and that the problem was in the other stuff. :) – matt Jul 23 '18 at 13:16