Last active
August 29, 2015 13:57
-
-
Save AliSoftware/9528562 to your computer and use it in GitHub Desktop.
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
@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