Dynamically generate label's frame to match it's content

On a project I had to come up with a way of dynamically adjusting a label's frame to match the content that was inside it. I first thought of using a uitextview instead of a label as my data could very well be multi-lined but decided against it when I discovered this approach. I decided to create a category to extend the behavior of the UILabel class to do this for me. The category .h file:

@interface UILabel(Size)

-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)width withStartXPosition:(CGFloat)startX withHeight:(CGFloat)maxHeight;



and the .m file:

@implementation UILabel(Size)

-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX withHeight:(CGFloat)maxHeight{
    self.lineBreakMode = UILineBreakModeWordWrap;
    self.numberOfLines = 0; //instructs the label that contain any number of lines

    CGFloat labelHeight = [self getHeightBasedOnWidth:maxWidth];
    CGFloat padding = ((maxHeight - labelHeight)/2); //center label within maxHeight box
    [self setFrame:CGRectMake(startX, padding, maxWidth, labelHeight)];


    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(maxWidth, 9999) lineBreakMode:self.lineBreakMode];
    //the 9999 here is used to indicate that it can a very long label if need be

    return size.height;


In order to use this method you need to have set-up your label with a font