根据UITableViewCell Label的高度和宽度调整背景图像的大小
我可以根据文本调整自定义UITablleViewCell
"标签的宽度和高度.看起来像下面.
I am able to adjust the Custom UITablleViewCell
label width and height based on the text. It looks like below.
现在,我想将下面的聊天气泡图像设置为标签作为背景,以便气泡根据标签文本调整其大小,并提供与其他Messenger相似的气泡效果.
Now i want to set the below chat bubble image to the label as background so that the bubble adjusts its size based on label text and gives the bubble effect similar to other messengers.
下面是我用于将图像设置为标签背景的代码.
Below is my code for setting the image as background of label.
-(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及其约束
For more info below is my CustomTableViewCell and its constraints
我为实现输出做了很多尝试,但我无法弄清楚.还有其他实现输出的方法,我准备遵循适合我要求的任何方法.任何帮助将不胜感激.
I tried a lot to achieve the output but i am unable to figure out. Are there any other approaches to achieve the output, i am ready to follow any approach which suits my requirement. Any help will be really appreciated.
要解决此问题,可以将所有 imageView约束设置为零. 然后,您可以将图像添加到.xassets文件中,然后使用切片图像.这将在必要时拉伸图像并缩小图像.
To solve this problem, you can set all the imageView constraints to zero. Then you can add the image in the .xassets file and then use slicing the image. This will stretch the image when necessary and shrink it.
切片取决于您.
Slicing is upto you.
希望它会对您有所帮助.
Hope it will help you.