【发布时间】:2017-04-18 11:21:56
【问题描述】:
我可以根据文本调整自定义UITablleViewCell 标签的宽度和高度。如下所示。
现在我想将下面的聊天气泡图像设置为标签作为背景,以便气泡根据标签文本调整其大小,并提供类似于其他信使的气泡效果。
以下是我将图像设置为标签背景的代码。
-(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 及其约束的更多信息
我尝试了很多来实现输出,但我无法弄清楚。是否有任何其他方法可以实现输出,我准备遵循任何适合我要求的方法。任何帮助将不胜感激。
【问题讨论】:
-
查看此链接可能会对您有所帮助:stackoverflow.com/questions/31823130/…
-
您可以查看此项目作为参考:github.com/jessesquires/JSQMessagesViewController。可能对您有所帮助。这个控件很好地拉伸了图像。
-
您需要将相同颜色的背景设置为 label 而不是 image 。并给予该标签大于等于的约束。
标签: ios objective-c iphone uitableview