8

I am trying to show a variable height amount of text through UILabel.
I set everything up through a combination of Xcode's storyboard and coding directly into the 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 have tried changing the wordwrap functions and tried playing around with a lot of different settings but to no avail.

Is there something specific that needs to be done in the storyboard?
Is the structure of my view->label important?
I have it so that the labels are in a view (which takes up the whole screen).

1
  • Take a look at this post for setting multiline labels Feb 7, 2013 at 13:10

6 Answers 6

13

I'm using SWIFT and I faced the same issue. I fixed it after I added layoutIfNeeded(). My code is as follows:

self.MyLabel.sizeToFit()
self.MyLabel.layoutIfNeeded()
10

You do not need to calculate the heights manually. For 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];
2
  • I'm developing for iOS 7 and this doesn't work. Maybe there are circumstances where it does, but in my case, with everything set the way you have it, plus a background color and a larger font, it doesn't work. It simply adds the ellipsis.
    – d2burke
    Mar 31, 2014 at 21:17
  • Setting the height of label to 0 on storyboard did it for me in swift. Setting the frame in code had not effect.
    – hablema
    Sep 18, 2014 at 15:24
1

Here is my solution for any problem such as 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 is, in my opinion the safest way to determine the needed height. Also, as far as I know, calling sizeToFit will not change it's size if it is already big enough to hold all the text.(will not make it smaller...just bigger if it needs to but it will not add more lines)

0

It doesn't look like you are setting lineBreakMode.

I got some code from stackoverflow way back when and put it into a category, you can about it 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 like this:

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

I tried to create the same thing and it works completely fine with me. (I don't have storyboard in my app but i don't think it 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

I have the troubel too,my code like this:

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

    [tagBtn.titleLabel sizeToFit];

TeacherTagBtn subclass of UIbutton you knows! then i want to get the width of label like this:

tempBtn.titleLabel.frame.size.width

Finally, I failed. the 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];

only add frame, it works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.