2

I have a UILabel with no of lines = 1 and auto shrink set to a minimum font size of 9. The font size of the label is system 70.0

enter image description here

I have increased the content hugging priority to 1000. I did this assuming that the height of the label will fit itself to the text.

enter image description here

But still the UILabel does not resize itself to fit it contents. The height of the label is very large. I want the height of the label to just fit it contents.

enter image description here

Thanks.

Naveen Ramanathan
  • 2,166
  • 1
  • 19
  • 21
  • Have you tried adding a constraint on the vertical size? e.g. a constrain to make your label fill the height of the container, but with priority = 500 so that content hugging takes precedence. – Clafou Jan 26 '16 at 12:54
  • @Clafou I tried adding a height constraint and setting the priority to 500 but the result is the same – Naveen Ramanathan Jan 26 '16 at 13:01
  • @hackphone Please edit your question. What you ask is unclear, you should make your question an actual question, not statements. – Crazyrems Jan 26 '16 at 13:28
  • @Crazyrems Added more details and I have made my question clear – Naveen Ramanathan Jan 26 '16 at 13:33

1 Answers1

1

The problem is you have given both leading and trailing space, so label will stretch itself to satisfy these constraints, As label has intrinsic size(i.e it calculates size based on its content), you just need to give constraint for x and y position

So, delete your leading and trailing space constraint, just have Align CenterX and Align CenterY constraints, these will be sufficient to give x and y position of your label and you will get desired results.

Edit - Adding Screenshot.

Also understand that here I have given trailing space constraint >= 10 so that label can resize itself according to its content.

Also if your are checking in iphone 6 screen then AutoShrink Minimum Font Size - 9 will not be fit for screen width, try giving AutoShrink Minimum Font Size - 6.

enter image description here

Result of above constraint -

1.For Long text

enter image description here

  1. For small text -

enter image description here

Ajay Kumar
  • 1,807
  • 18
  • 27
  • I have deleted my leading and trailing space constraint. Now the font size has increased and the text does not fit the UILabel. I want the text to fit the label and at the same time want the label to resize itself. You can check the output here http://postimg.org/image/cbzy7puzr/ – Naveen Ramanathan Jan 26 '16 at 12:53
  • Actually what is happening is your UIlabel size is growing according to its content,because you have given Align centerX for x coordinate. Solution for this is remove your Align CenterX constraint, add add back your leading and trailing constraint and have AutoShrink to minimum Font size. One important point for Trailing space have constraint as **Trailing space >= 10** – Ajay Kumar Jan 26 '16 at 13:04
  • tried removing the center x for the label, now the output is the same as stated in the question. The label does not resize itself. – Naveen Ramanathan Jan 26 '16 at 13:10
  • Tried as it is in the screenshot. I am running it on an ipad simulator. The output is the same. – Naveen Ramanathan Jan 26 '16 at 13:20
  • can you try increasing the height of your label and giving it a background color. I guess the text will be aligned to the bottom. – Naveen Ramanathan Jan 26 '16 at 13:23
  • Please increase the font size of your label (may be to 70) so that the label height increases. Then give the label a background color. Now add a long text so that the font size decreases automatically. I guess the text will be aligned to the bottom as my screen shot in the question. – Naveen Ramanathan Jan 26 '16 at 13:31
  • As you can see in your screenshot, the height of the blue background is bigger than the height of the text in the label. So the text seems to be aligned to the bottom of the label. I want the label to just fit the height of the text it contains. – Naveen Ramanathan Jan 26 '16 at 13:36
  • For that case Label don't have any property like adjustsFontSizeToFitHeight as we have for Width, you will have to write some manual code for that, you can take help from this link - https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/220 and this one http://stackoverflow.com/questions/2844397/how-to-adjust-font-size-of-label-to-fit-the-rectangle – Ajay Kumar Jan 26 '16 at 13:46
  • thanks for the links. Is there a way to do this using autolayout without manually calculating the height? – Naveen Ramanathan Jan 26 '16 at 13:50