Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (id)init
- {
- self = [super init];
- self.translatesAutoresizingMaskIntoConstraints = NO;
- self.numberOfLines = 2;
- self.backgroundColor = UIColorFromARGB(0x99000000);
- self.textColor = [UIColor whiteColor];
- self.font = [UIFont boldSystemFontOfSize:14.0f];
- return self;
- }
- - (void)drawTextInRect:(CGRect)rect {
- UIEdgeInsets insets = {2, 7, 2, 7};
- return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
- }
- - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
- {
- UIEdgeInsets insets = {2, 7, 2, 7};
- return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,insets) limitedToNumberOfLines:numberOfLines];
- }
- @implementation InsetLabel
- - (void) setInsets:(UIEdgeInsets)insets
- {
- _insets = insets ;
- [self invalidateIntrinsicContentSize] ;
- }
- - (void)drawTextInRect:(CGRect)rect
- {
- return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
- }
- - (void)resizeHeightToFitText
- {
- CGRect frame = [self bounds];
- CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);
- CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
- frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
- self.frame = frame;
- }
- - (CGSize) intrinsicContentSize
- {
- CGSize superSize = [super intrinsicContentSize] ;
- superSize.height += self.insets.top + self.insets.bottom ;
- superSize.width += self.insets.left + self.insets.right ;
- return superSize ;
- }
- @end
- @implementation InsetLabel
- - (void) setInsets:(UIEdgeInsets)insets
- {
- _insets = insets ;
- [self invalidateIntrinsicContentSize] ;
- }
- - (void)drawTextInRect:(CGRect)rect
- {
- return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
- }
- - (void)resizeHeightToFitText
- {
- CGRect frame = [self frame];
- CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);
- CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
- frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
- self.frame = frame;
- }
- - (CGSize) intrinsicContentSize
- {
- CGSize superSize = [super intrinsicContentSize] ;
- superSize.height += self.insets.top + self.insets.bottom ;
- superSize.width += self.insets.left + self.insets.right ;
- return superSize ;
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- [self resizeHeightToFitText];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement