Wednesday 11 September 2013

How to resize UILabel in code

How to resize UILabel in code

I have got a UILabel which is placed in between two UITableViews inside a
scrollview. What I want is to resize the UILabel height depending upon the
total text , which gets generated at runtime.
I have a solution but it doesnt work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
CGRect titleLabelBounds = self.lblPreparation.bounds;
titleLabelBounds.size.height = CGFLOAT_MAX;
// Change limitedToNumberOfLines to your preferred limit (0 for no limit)
CGRect minimumTextRect = [self.lblPreparation
textRectForBounds:titleLabelBounds limitedToNumberOfLines:0];
CGFloat titleLabelHeightDelta = minimumTextRect.size.height -
self.lblPreparation.frame.size.height;
CGRect titleFrame = self.lblPreparation.frame;
titleFrame.size.height += titleLabelHeightDelta;
self.lblPreparation.frame = titleFrame;
lblPreparation.text = @"sdjshdjshd sjhdjdd jjsjjsdhjsd \njsdhjshjshdjs
djs djsdh";
}
This above solution was proposed in a question but doesn't seem to be
working for me. Please suggest a suitable way.

No comments:

Post a Comment