Created
March 4, 2016 10:33
-
-
Save gblazex/0080ea3969dc26e887a5 to your computer and use it in GitHub Desktop.
BLZLabelWithInset - simple UILabel subclass with inset
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
@interface BLZLabelWithInset : UILabel | |
@property (nonatomic) UIEdgeInsets insets; | |
@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
@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