Created
December 28, 2014 19:01
-
-
Save slabko/da24f2cc78aab0b67cdf to your computer and use it in GitHub Desktop.
UILabel with margins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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