UILabel sizeToFit is not working properly. Using setNumberOfLines does not work. Requires variable height function to work

I am trying to show a variable height for text text via UILabel.
I tuned everything using a combination of Xcode storyboard and encoding directly into implementation files.

Here is the code:

CGRect labelFrame = CGRectMake(20, 20, 280, 800); descriptionLabel.frame = labelFrame; NSString *description = self.spell[@"description"]; descriptionLabel.text = description; [descriptionLabel setNumberOfLines:0]; [descriptionLabel sizeToFit]; 

I tried changing the wordwrap functions and tried to play with a lot of different settings, but to no avail.

Is there anything specific that needs to be done in the storyboard?
Is the structure of my view-> labels important?
I have it so that the labels are in the view (which occupies the entire screen).

+6
source share
6 answers

You do not need to calculate heights manually. For a variable height, set the height of the label to 0 before calling sizeToFit like this.

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)]; label.numberOfLines = 0; label.text = @"Some long piece of text..."; [label sizeToFit]; 
+9
source

I am using SWIFT and I am facing the same problem. I fixed it after adding layoutIfNeeded() . My code is as follows:

 self.MyLabel.sizeToFit() self.MyLabel.layoutIfNeeded() 
+5
source

Here is my solution for any problem like yours (dynamic height):

 NSString *description = @"Some text"; CGSize dynamicSize = [[description sizeWithFont:[UIFont fontWithName:@"FONT_NAME" size:14] constrainedToSize:CGSizeMake(300, 10000)]; //the width should be the one you need and just put in a very big height so that anything you would put in here would fit //dynamicSize is now the exact size you will need, with the fixed width and the height just enough so that all the text will fit. UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake:(0, 0, dynamicSize.width, dynamicSize.height)]; someLabel.font = YOUR_FONT; someLabel.numberOfRows = 10 //You should calculate how many rows you need by dividing dynamicSize.height to the height of one row (should also take into account the padding that your UILabel will put between rows 

This, in my opinion, is the safest way to determine the required height. Also, as far as I know, calling sizeToFit will not resize it if it is already large enough to hold all the text. (Will not reduce its size ... just more if necessary, but it will not add more lines)

+1
source

I tried to create the same thing, and everything is fine with me. (I do not have a storyboard in my application, but I do not think this will affect anything.)

 UILabel *bioLabel = [[UILabel alloc] init]; CGRect labelFrame = CGRectMake(0, 100, 320, 500); bioLabel.frame = labelFrame; NSString *bio =@ "YOUR_TEXT_HERE"; bioLabel.text = description; [bioLabel setNumberOfLines:0]; [bioLabel sizeToFit]; [self.view addSubview:bioLabel]; 
0
source

It doesn't look like you are setting lineBreakMode.

I got some code from stackoverflow when I put it in a category, you can here: http://www.notthepainter.com/multi-line-uilabel/

But if you just want the code, here it is:

 @interface UILabel (BPExtensions) - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; @end @implementation UILabel (EnkiExtensions) - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth {    if (fixedWidth < 0) {        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);    } else {        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);    }    self.lineBreakMode = UILineBreakModeWordWrap;    self.numberOfLines = 0;    [self sizeToFit]; } @end 

You call it this way:

 helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad."; [helpLabel_ sizeToFitFixedWidth: desiredWidth ]; 
0
source

I have a problem too, my code is like this:

  TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init]; [tagBtn setTitle:tagModel.values forState:UIControlStateNormal]; [tagBtn.titleLabel sizeToFit]; 

Subclass of TeacherTagBtn UIbutton you know! then I want to get the label width as follows:

 tempBtn.titleLabel.frame.size.width 

Finally, I failed. width is 0. so like this:

  TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)]; [tagBtn setTitle:tagModel.values forState:UIControlStateNormal]; [tagBtn.titleLabel sizeToFit]; 

just add a frame, it works great!

0
source

All Articles