0

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

Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
  • A few more details would be preferable. Are you using a tableView? If so, are you using a custom tableViewCell Nib, or are you simply using the default cells? – ZGski Mar 01 '16 at 17:05
  • No... no tableView... Although I do see that so many are using that approach, that perhaps I should reconsider using a simple view. Thanks! – Martin Muldoon Mar 01 '16 at 17:17

1 Answers1

2

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;
Community
  • 1
  • 1
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73