Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IPInsetLabel: a simple UILabel subclass that adds padding insets and auto-height-resizing
//
// IPInsetLabel.h
// Instapaper
//
// Created by Marco Arment on 7/23/11.
// Copyright 2011 Instapaper LLC, released to the public domain.
//
#import <UIKit/UIKit.h>
@interface IPInsetLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets insets;
- (void)resizeHeightToFitText;
@end
//
// IPInsetLabel.m
// Instapaper
//
// Created by Marco Arment on 7/23/11.
// Copyright 2011 Instapaper LLC, released to the public domain.
//
#import "IPInsetLabel.h"
@implementation IPInsetLabel
@synthesize insets;
- (void)drawTextInRect:(CGRect)rect
{
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}
- (void)resizeHeightToFitText
{
CGRect frame = [self bounds];
CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);
CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
self.frame = frame;
}
@end
@jeebster

This comment has been minimized.

Show comment
Hide comment
@jeebster

jeebster May 11, 2012

This is a great method, Marco!

Perhaps one suggestion: setting the frame to the instance's bounds prevents explicit instructions for points of origin. This seems to work well if you need to pass in origins in initWithFrame:

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, frame.size.width, frame.size.height);

jeebster commented May 11, 2012

This is a great method, Marco!

Perhaps one suggestion: setting the frame to the instance's bounds prevents explicit instructions for points of origin. This seems to work well if you need to pass in origins in initWithFrame:

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, frame.size.width, frame.size.height);
@mokagio

This comment has been minimized.

Show comment
Hide comment
@mokagio

mokagio commented Jan 7, 2013

+1

@jlamimoso

This comment has been minimized.

Show comment
Hide comment
@jlamimoso

jlamimoso Jun 4, 2013

Useful class ! Thanks

jlamimoso commented Jun 4, 2013

Useful class ! Thanks

@trant

This comment has been minimized.

Show comment
Hide comment
@trant

trant Jun 17, 2013

I have done a small change in order to keep the original origin ( so that the label doesn't move from place when the method is used )

  • (void)resizeHeightToFitText
    {

    //save initial origin position
    CGPoint origin = self.frame.origin;

    CGRect frame = [self bounds];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
    frame.size.height = newSize.height + self.insets.top + self.insets.bottom;

    frame.origin = origin;
    self.frame = frame;
    }

trant commented Jun 17, 2013

I have done a small change in order to keep the original origin ( so that the label doesn't move from place when the method is used )

  • (void)resizeHeightToFitText
    {

    //save initial origin position
    CGPoint origin = self.frame.origin;

    CGRect frame = [self bounds];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
    frame.size.height = newSize.height + self.insets.top + self.insets.bottom;

    frame.origin = origin;
    self.frame = frame;
    }

@techinpark

This comment has been minimized.

Show comment
Hide comment
@techinpark

techinpark Dec 4, 2013

Thanks is very useful class :)

techinpark commented Dec 4, 2013

Thanks is very useful class :)

@bomalley

This comment has been minimized.

Show comment
Hide comment
@bomalley

bomalley Mar 16, 2014

Looks like this class needs an update for iOS 7. Xcode is dumping out a warning about the use of a deprecated method:

IPInsetLabel.m:24:33: 'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithSize:options:attributes:context:

bomalley commented Mar 16, 2014

Looks like this class needs an update for iOS 7. Xcode is dumping out a warning about the use of a deprecated method:

IPInsetLabel.m:24:33: 'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithSize:options:attributes:context:
@danomatika

This comment has been minimized.

Show comment
Hide comment
@danomatika

danomatika Jul 9, 2014

Here's the update for the iOS 7 deprecation noted by @bomalley. I've also included the origin fix by @jeebster:

- (void)resizeHeightToFitText {
    CGRect frame = [self bounds];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGRect newSize = [self.text boundingRectWithSize:CGSizeMake(textWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil];

    frame.size.height = newSize.size.height + self.insets.top + self.insets.bottom;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, frame.size.width, frame.size.height);
}

danomatika commented Jul 9, 2014

Here's the update for the iOS 7 deprecation noted by @bomalley. I've also included the origin fix by @jeebster:

- (void)resizeHeightToFitText {
    CGRect frame = [self bounds];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGRect newSize = [self.text boundingRectWithSize:CGSizeMake(textWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil];

    frame.size.height = newSize.size.height + self.insets.top + self.insets.bottom;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, frame.size.width, frame.size.height);
}
@doublerebel

This comment has been minimized.

Show comment
Hide comment
@doublerebel

doublerebel Oct 14, 2014

Thanks all for your work! I just pushed a CocoaPod out for this code up to @danomatika's fixes, with additions from @soffes/SAMLabel. It can be found at @doublerebel/IPInsetLabel or on Cocoapods as IPInsetLabel. Hope to see this lib continue, glad to accept any PRs!

doublerebel commented Oct 14, 2014

Thanks all for your work! I just pushed a CocoaPod out for this code up to @danomatika's fixes, with additions from @soffes/SAMLabel. It can be found at @doublerebel/IPInsetLabel or on Cocoapods as IPInsetLabel. Hope to see this lib continue, glad to accept any PRs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment