Skip to content

Instantly share code, notes, and snippets.

@ryandevore
Created March 19, 2014 21:35
Show Gist options
  • Save ryandevore/9651893 to your computer and use it in GitHub Desktop.
Save ryandevore/9651893 to your computer and use it in GitHub Desktop.
//
// UILabel+UUResizing.h
// Useful Utilities - UILabel resizing extensions
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: ryan@threejacks.com
//
// These methods are handy when dynamically resizing and positions labels in
// a subview. Typically used in a UITableViewCell where you have a label
// that you want to call sizeToFit on. Before calling sizeToFit you need
// to reset the frame to the original frame, or the sizeToFit will not
// behave as expected.
//
#import <UIKit/UIKit.h>
@interface UILabel (UUResizing)
- (void) uuResizeWidth;
- (void) uuResizeWidthAndHeight;
@end
//
// UILabel+UUResizing.m
// Useful Utilities - UILabel resizing extensions
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: ryan@threejacks.com
#import "UILabel+UUResizing.h"
#import <objc/runtime.h>
#define kUILabelUUResizingOriginalFrameKey @"UILabelUUResizingOriginalFrameKey"
@implementation UILabel (UUResizing)
- (CGRect) uuOriginalFrame
{
NSValue* val = objc_getAssociatedObject(self, kUILabelUUResizingOriginalFrameKey);
if (!val)
{
val = [NSValue valueWithCGRect:self.frame];
objc_setAssociatedObject(self, kUILabelUUResizingOriginalFrameKey, val, OBJC_ASSOCIATION_RETAIN);
}
return [val CGRectValue];
}
- (void) uuResizeWidth
{
CGRect originalFrame = [self uuOriginalFrame];
self.frame = originalFrame;
[self sizeToFit];
CGRect f = originalFrame;
f.size.width = self.frame.size.width;
self.frame = f;
}
- (void) uuResizeWidthAndHeight
{
self.frame = [self uuOriginalFrame];
[self sizeToFit];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment