I have two UILabel
in side of UICell
, which contains dynamic text so i need to resize its frame according to content, for which im using [str
Try This code.
label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = @"some long text";
[label sizeToFit];
NSLog(@"Label's frame is: %@", NSStringFromCGRect(label.frame));
For reference : click here
Instead of doing your own customization. Try with dynamic uitableviewcell height. You will find really usefull & comparatively easier than what you have done.
You can check it here : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full of explanation. Enjoy programming.
NSString *reuseIdentifier = @"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
cell=nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;
}
In your case you need to set the height of cell based on the label.
Check the link :
http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/
There is a function named
-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
Use that function to calculate height as :
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat _height = [self getHeightForLabel:self.label.text font:[self.label font]];
return _height;
}
and do the same while creating and adding label in the cell. I hope this will help.