Reputation: 3438
I'm trying to consume blog posts from a website and display them in an iPhone app. The title and subtitle can be multi-line.
I can't figure how to change the height of the labels based on content.
I'm able to achieve this using a UITextView, but is this the proper use of this object?
Thanks! ~M
Upvotes: 0
Views: 752
Reputation: 3519
Definitely you can change the height of a UILabel dynamically based on the text. Just make sure that the UILabel has numberOfLines = 0 and follow the answer from this example: ios dynamic sizing labels
For a complete answer, here is the code that you can do it with...
Calculate the height of your text that will be in your label, then set the frame of your label based on that size:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
Upvotes: 2