// | |
// 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 |
// | |
// 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 |
This comment has been minimized.
This comment has been minimized.
+1 |
This comment has been minimized.
This comment has been minimized.
Useful class ! Thanks |
This comment has been minimized.
This comment has been minimized.
I have done a small change in order to keep the original origin ( so that the label doesn't move from place when the method is used )
|
This comment has been minimized.
This comment has been minimized.
Thanks is very useful class :) |
This comment has been minimized.
This comment has been minimized.
Looks like this class needs an update for iOS 7. Xcode is dumping out a warning about the use of a deprecated method:
|
This comment has been minimized.
This comment has been minimized.
Here's the update for the iOS 7 deprecation noted by @bomalley. I've also included the origin fix by @jeebster:
|
This comment has been minimized.
This comment has been minimized.
Thanks all for your work! I just pushed a CocoaPod out for this code up to @danomatika's fixes, with additions from @soffes/SAMLabel. It can be found at @doublerebel/IPInsetLabel or on Cocoapods as IPInsetLabel. Hope to see this lib continue, glad to accept any PRs! |
This comment has been minimized.
This is a great method, Marco!
Perhaps one suggestion: setting the frame to the instance's bounds prevents explicit instructions for points of origin. This seems to work well if you need to pass in origins in initWithFrame: