Skip to content

Instantly share code, notes, and snippets.

@slabko
Created December 28, 2014 19:01
Show Gist options
  • Save slabko/da24f2cc78aab0b67cdf to your computer and use it in GitHub Desktop.
Save slabko/da24f2cc78aab0b67cdf to your computer and use it in GitHub Desktop.
UILabel with margins
#import <UIKit/UIKit.h>
@interface ARLMarginLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets margins;
// For IB
@property (nonatomic, assign) IBInspectable CGFloat topMargin;
@property (nonatomic, assign) IBInspectable CGFloat rightMargin;
@property (nonatomic, assign) IBInspectable CGFloat bottomMargin;
@property (nonatomic, assign) IBInspectable CGFloat leftMargin;
@end
#import "ARLMarginLabel.h"
IB_DESIGNABLE
@implementation ARLMarginLabel
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.margins)];
}
- (CGSize)intrinsicContentSize
{
CGSize size = [super intrinsicContentSize];
size.width += self.leftMargin + self.rightMargin;
size.height += self.topMargin + self.bottomMargin;
return size;
}
- (UIEdgeInsets)margins
{
return UIEdgeInsetsMake(self.topMargin, self.leftMargin,
self.bottomMargin, self.rightMargin);
}
- (void)setMargins:(UIEdgeInsets)margins
{
self.topMargin = margins.top;
self.leftMargin = margins.left;
self.bottomMargin = margins.bottom;
self.rightMargin = margins.right;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment