Skip to content

Instantly share code, notes, and snippets.

@AliSoftware
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliSoftware/9528562 to your computer and use it in GitHub Desktop.
Save AliSoftware/9528562 to your computer and use it in GitHub Desktop.
@interface MyCell : UITableViewCell
+(CGSize)cellSizeForText:(NSString*)text;
@end
#import "UIView+NFCore.h" // "instantiateFromNib:"
@implementation MyCell
static CGSize const kCellMargins = (CGSize) { .width = 20, .height = 20 };
+(CGSize)cellSizeForText:(NSString*)text
{
static CGSize cellSizeXIB;
static CGFloat labelSizeXIB;
static UIFont* labelFontXIB;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
MyCell *dummyCell = [self instantiateFromNib:cellNib];
cellSizeXIB = dummyCell.bounds.size;
labelSizeXIB = dummyCell.mylabel.frame.size;
});
CGSize labelSize = [text sizeWithFont:labelFontXIB constrainedToSize:labelSizeXIB.width];
// Keep the margins between the label and the cell from the XIB, adjust only height
return CGSizeMake(cellSizeXIB.width , cellSizeXIB.height + (labelSize.height - labelSizeXIB.height));
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment