Skip to content

Instantly share code, notes, and snippets.

@gblazex
Created March 4, 2016 10:33
Show Gist options
  • Save gblazex/0080ea3969dc26e887a5 to your computer and use it in GitHub Desktop.
Save gblazex/0080ea3969dc26e887a5 to your computer and use it in GitHub Desktop.
BLZLabelWithInset - simple UILabel subclass with inset
@interface BLZLabelWithInset : UILabel
@property (nonatomic) UIEdgeInsets insets;
@end
@implementation BLZLabelWithInset
- (instancetype)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame])) return nil;
self.insets = UIEdgeInsetsMake(0, 0, 0, 0);
return self;
}
- (void)setInsets:(UIEdgeInsets)insets
{
_insets = insets;
[self invalidateIntrinsicContentSize];
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}
- (CGSize)intrinsicContentSize
{
return [self sizeByAddingInsetsTo:[super intrinsicContentSize]];
}
- (CGSize)sizeThatFits:(CGSize)size
{
return [self sizeByAddingInsetsTo:[super sizeThatFits:size]];
}
- (CGSize)sizeByAddingInsetsTo:(CGSize)size
{
size.height += self.insets.top + self.insets.bottom;
size.width += self.insets.left + self.insets.right;
return size;
}
- (CGRect)textRectForBounds:(CGRect)bounds
limitedToNumberOfLines:(NSInteger)numberOfLines
{
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.insets)
limitedToNumberOfLines:numberOfLines];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment