Reputation: 87
I using below code in my application to get dynamic height for UILabel.
CGSize maximumLabelSize = CGSizeMake(231, FLT_MAX);
CGSize expectedLabelSize = [labelString
sizeWithFont:self.verbLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:self.verbLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = self.verbLabel.frame;
newFrame.size.height = expectedLabelSize.height;
self.verbLabel.frame = newFrame;
return newFrame;
My labelString is What if you asked a controversial public figure? What ideas might he/she suggest? So sometimes I'm able to show this whole string but sometimes it just cut the some text. How can I solve this ... please help me and let me know If am doing something wrong.
Upvotes: 0
Views: 268
Reputation: 3055
i'd advise you to use the following approach instead of the sizeWithFont:
thing:
CGSize maximumLabelSize = CGSizeMake(231, FLT_MAX);
CGSize requiredSize = [self.verbLabel sizeThatFits: maximumLabelSize];
self.verbLabel.frame = CGRectMake(x, y, requiredSize.width, requiredSize.height);
This is much cleaner and will probably work better for you!
setting self.verbLabel.numberOfLines = 0;
may be also beneficial.
Here's a good explanation why using sizeThatFits
is better than using sizeWithFont
.
Upvotes: 1
Reputation: 17585
Set numberOfLines to 0 to allow for any number of lines.
label.numberOfLines = 0;
Upvotes: 2