0

I'm trying to add a UILabel at the top of my views which can be multiple lines. I've researched SO but I can't get it to work since it will only show one line and I want it to be as big as it needs to be. I'm doing this with auto layout and the current code I have is this:

UILabel *label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"jklljk sdkhfdjkdsfjhkfk fhs fdh fk dksdks  dfss s dfs dfs  fsdkdfks dfks dfks df k dfh";
label.numberOfLines = 0;

NSDictionary *views = @{@"label" : label};
[self.view addSubview:label];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label(>=20)]" options:0 metrics:nil views:views]];
Haagenti
  • 7,974
  • 5
  • 38
  • 52
  • if you want it to have multiple lines, you have to make `numberOfLines` bigger than 0 – user2277872 Feb 18 '14 at 15:48
  • 2
    I have set it to zero because I do not how many there will be exactly and I have read here somewhere that setting it to zero will take care of it – Haagenti Feb 18 '14 at 15:49
  • http://stackoverflow.com/questions/18315441/with-what-should-i-replace-the-deprecated-sizewithfontcontrainedtosizelinebrea ?? – Totumus Maximus Feb 18 '14 at 15:55
  • @MouNtant your snippet is working for me, don't know why do you have this problem. PS: without label.numberOfLines = 0; not working – Dimentar Feb 18 '14 at 15:59

3 Answers3

0

You need to set a frame on the label to set it's initial position in the view.

From the View Programming guide:

When adding a subview to its parent, the subview’s current frame rectangle denotes its initial position inside the parent view.

André
  • 51
  • 4
0

Try calling:

[self.view layoutIfNeeded];

after you add the constraints. That should update the view with the new constraints.

coder
  • 10,460
  • 17
  • 72
  • 125
0

If you can add the label via the interface builder it will save writing a lot of the label attribute code, though from what you've described, if you use the code below (substituting your desired position, size and text values) then this label will change according to the content you populate it with. I use it all the time, works perfectly.

UILabel *label = [[UILabel alloc] initWithFrame:GCRectMake(0,0,300, 50)]; //these values to be changed to reflect where you want the label to appear, initial position and size, width etc.
//setting up the label attributes etc
label.numberOfLines = 0; //This means there's no limit to lines of text.
label.font = [UIFont systemFontOfSize:13];
label.textColor = [UIColor blackColor];

NSString *content = YOUR_TEXT;// your example @"jklljk sdkhfdjkdsfjhkfk fhs fdh fk dksdks  dfss s dfs dfs  fsdkdfks dfks dfks df k dfh";

CGSize maximumLabelSize = CGSizeMake(300, 1000); //For example - the height can be changed to any maximum value.

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName]; //This allows a calculation to be made of the space taken up, so if you're using a custom or large font it will calculate accordingly.

CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

CGRect frame = label.frame;
frame.size.height = newExpectedLabelSize.height;
label.frame = frame;  //This last line should change the height of your label according to what it needs to be to have all the text visible and over multiple lines.

I hope this helps you with what you're looking for.

(This replaces the need for any of the constraint coding you had too.)

Cheers, Jim

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48