Skip to content

Instantly share code, notes, and snippets.

@xslim
Created March 16, 2011 22:52
Show Gist options
  • Save xslim/873494 to your computer and use it in GitHub Desktop.
Save xslim/873494 to your computer and use it in GitHub Desktop.
Get max mini-cell width
// Beta !!
- (NSArray *)widthForData:(NSArray *)dataArray
{
if ([dataArray count] < 1) return nil;
CGFloat totalWidth = 0.0f;
UIFont *font = [UIFont systemFontOfSize:12.0f];
float maxWidth = 400.0f;
float minWidth = 20.0f;
int sCount = [[dataArray objectAtIndex:0] count];
float sSizes[sCount];
for (NSArray *stringArray in dataArray) {
int idx = 0;
for (NSString *s in stringArray) {
CGSize sSize = [s sizeWithFont:font constrainedToSize:CGSizeMake(maxWidth, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
float sWidth = sSize.width + 20.0f;
if (sWidth > sSizes[idx]) {
sSizes[idx] = (sWidth < maxWidth) ? (sWidth > minWidth) ? sWidth : minWidth : maxWidth;
}
idx++;
}
}
NSMutableArray *mA = [NSMutableArray arrayWithCapacity:sCount];
for (int i=0; i<sCount; i++) {
totalWidth += sSizes[i];
[mA addObject:[NSNumber numberWithFloat:sSizes[i]]];
}
//hack
CGRect tableFrame = self.tableView.frame;
tableFrame.size.width = (totalWidth + 30.0f);
self.tableView.frame = tableFrame;
return mA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment