-1

I read this answer in how to adjust the height of my label based on the text of the label: Vertically align text to top within a UILabel

This is my code trying to set new Text and adjust the label at the same time.

But what I find out is the height of the label never get changed. It ways stays at the value I set in my xib file. I use tool, Spark to inspect the height of the label:

-(void) setNewText(NSString *)newText
{
     self.myLabel.text = newText;
     self.myLabel.numberOfLines = 0;
     [self.myLabel sizeToFit];
     [self setNeedsLayout];
}

Can you please tell me what's wrong with my code?

Thank you.

Updated:

I have tried @rdelmar suggestion. Set the width constraint for the label and num of line to 0. The height of the ui label does get adjusted. But there are extra spacing before and after the first/last line of the text.

enter image description here Here is the screenshot I captured using Spark. The blue rectangle is the UILabel. As you can see, there are extra spacing before/after the first/last line. How can I remove those spacing?

Community
  • 1
  • 1
n179911
  • 19,547
  • 46
  • 120
  • 162

1 Answers1

1

You don't need to do anything in code to get your label to adjust its height. In the xib or storyboard, give the label constraints to set its position, and give it a fixed width constraint. Do not set its height. Set the numberOfLines to 0, and that's all you need to do. If you set the text with a string that's too long to fit on one line, the label will adjust its height automatically.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks. But what width I should set the fixed width to? I want it to layout well in both iPhone 5/6 so the width of the label is different. – n179911 Jan 27 '15 at 20:08
  • @n179911, you can set the width several ways. If you want it to adjust to screen width, then give it spacing constraints to the left and right edges of the superview instead of a fixed width. – rdelmar Jan 27 '15 at 20:15
  • Thanks for your help. I tried what you suggested: Have 1 width constrain for the UILabel. set the number of Lines to 0. That is. The height of the Label does get adjust as you said. But there are extra spacing before/after the first and last line of the text. I have updated my question with the screenshot. – n179911 Jan 27 '15 at 23:24
  • @n179911, I don't see that extra space when I do the same thing, so I'm not sure what's going on. I've never used Spark, are you sure it's showing the correct frame? Try giving your label a background color, and see if it still looks like it has that extra space at runtime. – rdelmar Jan 27 '15 at 23:34
  • I tried setting the background color of my label to green. I still see extra space before the first line. – n179911 Jan 27 '15 at 23:46
  • @n179911, I can't duplicate your problem, so I don't know what else to say. – rdelmar Jan 28 '15 at 00:17
  • I only have 1 Label in my xib file. And other than the 1 width constraints that u suggested. I have 3 other constraints which set the horizontal and vertical spaces. that is. – n179911 Jan 28 '15 at 00:20
  • @n179911, What constraints do you have? You should only need one vertical and one horizontal. – rdelmar Jan 28 '15 at 00:22
  • Thanks. @rdelmar. I have 1 top vertical constraints. 1 leading horizontal constraints. 1 trailing horizontal constraints. – n179911 Jan 28 '15 at 03:53
  • @n179911, that sounds right, so I don't know why you're getting different results from me. Perhaps it's something with the font. What font and language is your text (I can't tell from your image). – rdelmar Jan 28 '15 at 05:08