Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

UILabel fit to text

I got an app, with label and big text. I tried to fit label's height for text like this:

UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(20, sizeInSpik, 275, 0)];
            description.text = spik.text;
            description.adjustsFontSizeToFitWidth = NO;
            description.autoresizingMask = UIViewAutoresizingFlexibleHeight;
            [description setFont:[UIFont fontWithName:@"Helvetica Neu" size:13]];
            description.textColor = [UIColor blackColor];
            description.lineBreakMode = UILineBreakModeWordWrap;
            description.numberOfLines = 0;
            [description sizeToFit];

            // Set the height
            CGSize maximumLabelSize1 = CGSizeMake(270,9999);
            CGSize titleSize1 = [description.text sizeWithFont:description.font constrainedToSize:maximumLabelSize1 lineBreakMode:description.lineBreakMode];

            //Adjust the label the the new height
            CGRect newFrame1 = description.frame;
            newFrame1.size.height = titleSize1.height;
            description.frame = newFrame1;
            description.backgroundColor = [UIColor greenColor];

But when i implement this code result look like this:

enter image description here

Green color is UILabel background.

What is the problem?

PS Besides font size isnt 13. Why?

Upvotes: 0

Views: 762

Answers (2)

Jayson Lane
Jayson Lane

Reputation: 2818

Are you sure "Helvetica Neu" is correct? I don't think it will appropriately size with an incorrect font. Try:

[UIFont fontWithName:@"HelveticaNeue" size:13];

Upvotes: 1

Sebastian Flückiger
Sebastian Flückiger

Reputation: 5555

you should really use a TextView if your text is so big - its made for it =)

UITextView Class Reference

Upvotes: 1

Related Questions