Monday 29 August 2011

Resizing TableViewCell according to dynamic text

When there is dynamic text and we do not know how much height a label should accommodate to show the complete text usually we do trail truncation.
In this blog I have discovered how to show the dynamic text in table cell and wants to share it with all of you.

Main Logic:-

//CGSize is a variable which will contains the height of label.
CGSize labelsize;

//Label Created
UILabel *commentsTextLabel = [[UILabel alloc] init];
//text string contains the text that will be displayed in the label

NSString *text=[[self.blogTextArray objectAtIndex:indexPath.row]capitalizedString];
[commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];

//height of the label is calculated and copied in labelsize

labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);


Logic Impemented:-

- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell=nil;
cell = [tbleView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
if([self.blogTextArray count]>0)
{
UILabel *commentsCreatedLabel = (UILabel *)[cell.contentView viewWithTag:54];
commentsCreatedLabel.text=[self.blogTitleArray objectAtIndex:indexPath.row];
UILabel *blogDateLabel = (UILabel *)[cell.contentView viewWithTag:60];
blogDateLabel.text=[self.blogDateArray objectAtIndex:indexPath.row];
UILabel *blogTitleLabel = (UILabel *)[cell.contentView viewWithTag:50];
blogTitleLabel.text=[self.blogTextArray objectAtIndex:indexPath.row];

}
else {
}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;
}

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath

{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
UILabel *commentsCreatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 100, 20)];
commentsCreatedLabel.tag =54;
[commentsCreatedLabel setBackgroundColor:[UIColor clearColor]];
[commentsCreatedLabel setFont:[UIFont fontWithName:@"Helvetica"size:12]];
[cell.contentView addSubview:commentsCreatedLabel];
[commentsCreatedLabel release];

UILabel *blogDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 2, 65, 20)];
blogDateLabel.tag =60;
//[blogDateLabel setBackgroundColor:[UIColor grayColor]];
[blogDateLabel setBackgroundColor:[UIColor clearColor]];
[blogDateLabel setFont:[UIFont fontWithName:@"Helvetica"size:12]];
[cell.contentView addSubview:blogDateLabel];
[blogDateLabel release];

CGSize labelsize;
UILabel *commentsTextLabel = [[UILabel alloc] init];;
commentsTextLabel.tag =50;
[commentsTextLabel setNumberOfLines:0];
[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text=[[self.blogTextArray objectAtIndex:indexPath.row]capitalizedString];
[commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
[cell.contentView addSubview:commentsTextLabel];
[commentsTextLabel release];


UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
deleteButton.contentMode = UIViewContentModeScaleAspectFill;
UIImage *newImage12 = [UIImage imageNamed:@"delete1.png"];
deleteButton.tag=indexPath.row;
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
[deleteButton addTarget:self action:@selector(deletePostMethod:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteButton];

}

heightForRowAtIndexPath method is important to write because unless this table view cell height will not change.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
CGSize labelsize;
UILabel * textDesc1 = [[UILabel alloc] init];
[textDesc1 setNumberOfLines:0];
textDesc1.text=[[self.blogTextArray objectAtIndex:indexPath.row]capitalizedString];
[textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(268, 2000.0)lineBreakMode:UILineBreakModeWordWrap];
labelsize.height=labelsize.height+35;
[textDesc1 release];
return (CGFloat)labelsize.height;
}

No comments:

Post a Comment