我可以根据文本调整自定义标签的宽度和高度UITablleViewCell
。它看起来像下面。根据UITableViewCell调整背景图像的大小标签的高度和宽度
现在我想设置低于聊天气泡图像标签作为背景,使泡沫调整基于标签文本的大小,并给出类似于其他使者的泡沫效应。
下面是我对图像设置为标签的后台代码。
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ChatConversationTableViewCell";
ChatConversationTableViewCell *cell = (ChatConversationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatConversationTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.chatmsgLabel.text = [chatHistoryArr objectAtIndex:indexPath.row];
cell.timeAndDateMsgLabel.text = [timeAndDateMsgArr objectAtIndex:indexPath.row];
UIImage *img = [UIImage imageNamed:@"bubble2.png"];
CGSize imgSize = cell.timeAndDateMsgLabel.frame.size;
UIGraphicsBeginImageContext(imgSize);
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.timeAndDateMsgLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
return cell;
}
输出类似于
我的预期输出是
欲了解更多信息下面是我的CustomTableViewCell和它的约束
我尝试了很多实现了输出,但我无法弄清楚。是否有任何其他方法来实现输出,我准备好遵循任何适合我的要求的方法。任何帮助将非常感激。
看到这个链接它可能会帮助你:http://stackoverflow.com/questions/31823130/how-to-let-a-uiimage-only-stretch-in-a-specific-area –
你可以查看这个项目作为参考:https://github.com/jessesquires/JSQMessagesViewController。可能对你有帮助。该控件以良好的方式拉伸图像。 – Aashish1aug
你需要设置相同的颜色背景来标签,而不是图像。并且给予该标签更严格的等同限制。 – KKRocks