简体   繁体   中英

How to make imageview as stretchable when imageview is added as subview of uitableviewcell

I want to make imageview as stretchable if i write more then two lines in table cell. code is as below:

Any help will be appriciated.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];
NSUInteger row = indexPath.row;
if (row < chatData.count)
{
    self.bubbleImage = [[UIImageView alloc] init];
    self.bubbleImage.frame = CGRectMake(0,22,250,66);
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];

    NSString *chatText = [[chatData objectAtIndex:row] objectForKey:TEXT];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
    cell.textString.frame = CGRectMake(75, 18, size.width +20, size.height + 20); // set text frame
    cell.textString.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];        // set text font
    cell.textString.text = chatText;                                              // set text
    [cell.textString sizeToFit];

    NSDate *theDate = [[chatData objectAtIndex:row] objectForKey:DATE];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:DATE_FORMAT];
    NSString *timeString = [formatter stringFromDate:theDate];
    cell.timeLabel.text = timeString;                                       // set timeLabel to display date and time
    cell.userLabel.text = [[chatData objectAtIndex:row] objectForKey:NAME]; // set userLabel to display userName

    [self.bubbleImage addSubview:cell.userLabel];
    [self.bubbleImage addSubview:cell.timeLabel];
    [self.bubbleImage addSubview:cell.textString];

    [cell addSubview:self.bubbleImage];
}
return cell;
}

Any help will be appreciated

You need to adjust the frame height of the bubbleImage. Try to make use of maximum Y of the textString inside the cell to calculate the final height of bubbleImage:

self.bubbleImage.frame = CGRectMake(0,22,250,CGRectGetMaxY(cell.textString.frame)+10.0);

Also remember to calculate the height for each row of the table view cell.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //calculate your height for each row based on the textString
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
    return size.height+20.0; //or whatever padding value you need
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM