Skip to content

Instantly share code, notes, and snippets.

@ShadoFlameX
Created May 7, 2013 20:21
Show Gist options
  • Save ShadoFlameX/5535784 to your computer and use it in GitHub Desktop.
Save ShadoFlameX/5535784 to your computer and use it in GitHub Desktop.
UILabel subclass supporting content edge insets via UIEdgeInsets property.
//
// BHInsetLabel.h
// Created by Bryan Hansen on 5/7/13.
//
#import <UIKit/UIKit.h>
@interface BHInsetLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets contentInsets;
@end
@implementation BHInsetLabel
#pragma mark - Layout
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize adjustedSize = [super sizeThatFits:size];
adjustedSize.width += self.contentInsets.left + self.contentInsets.right;
adjustedSize.height += self.contentInsets.top + self.contentInsets.bottom;
return adjustedSize;
}
- (void)drawTextInRect:(CGRect)rect
{
CGRect insetRect = CGRectMake(self.contentInsets.left,
self.contentInsets.top,
rect.size.width - self.contentInsets.left - self.contentInsets.right,
rect.size.height - self.contentInsets.top - self.contentInsets.bottom);
[super drawTextInRect:insetRect];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment