Created
May 8, 2012 22:22
-
-
Save markschabacker/2639906 to your computer and use it in GitHub Desktop.
IPInsetLabel: a simple UILabel subclass that adds padding insets and auto-height-resizing
This file contains 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
// | |
// IPInsetLabel.h | |
// Instapaper | |
// | |
// Created by Marco Arment on 7/23/11. | |
// Copyright 2011 Instapaper LLC, released to the public domain. | |
// | |
#import <UIKit/UIKit.h> | |
@interface IPInsetLabel : UILabel | |
@property (nonatomic, assign) UIEdgeInsets insets; | |
- (void)resizeHeightToFitText; | |
@end |
This file contains 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
// | |
// IPInsetLabel.m | |
// Instapaper | |
// | |
// Created by Marco Arment on 7/23/11. | |
// Copyright 2011 Instapaper LLC, released to the public domain. | |
// | |
#import "IPInsetLabel.h" | |
@implementation IPInsetLabel | |
@synthesize insets; | |
- (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; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment