Skip to content

Instantly share code, notes, and snippets.

@CoderXL
Forked from danielphillips/UILabel+dynamicSizeMe.h
Created February 13, 2014 00:25
Show Gist options
  • Save CoderXL/8967373 to your computer and use it in GitHub Desktop.
Save CoderXL/8967373 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Phillips revised this gist Aug 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UILabel+dynamicSizeMe.m
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #import "UILabel+dynamicSizeMe.h"

    @interface UILabel (dynamicSizeMe)
    @implementation UILabel (dynamicSizeMe)

    -(float)resizeToFit{
    float height = [self expectedHeight];
  2. Daniel Phillips created this gist Jun 2, 2011.
    6 changes: 6 additions & 0 deletions UILabel+dynamicSizeMe.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @interface UILabel (dynamicSizeMe)

    -(float)resizeToFit;
    -(float)expectedHeight;

    @end
    25 changes: 25 additions & 0 deletions UILabel+dynamicSizeMe.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #import "UILabel+dynamicSizeMe.h"

    @interface UILabel (dynamicSizeMe)

    -(float)resizeToFit{
    float height = [self expectedHeight];
    CGRect newFrame = [self frame];
    newFrame.size.height = height;
    [self setFrame:newFrame];
    return newFrame.origin.y + newFrame.size.height;
    }

    -(float)expectedHeight{
    [self setNumberOfLines:0];
    [self setLineBreakMode:UILineBreakModeWordWrap];

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width,9999);

    CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
    constrainedToSize:maximumLabelSize
    lineBreakMode:[self lineBreakMode]];
    return expectedLabelSize.height;
    }

    @end