2017-04-18 140 views
0

我可以根据文本调整自定义标签的宽度和高度UITablleViewCell。它看起来像下面。根据UITableViewCell调整背景图像的大小标签的高度和宽度

enter image description here

现在我想设置低于聊天气泡图像标签作为背景,使泡沫调整基于标签文本的大小,并给出类似于其他使者的泡沫效应。

enter image description here

下面是我对图像设置为标签的后台代码。

-(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; 


} 

输出类似于

enter image description here

我的预期输出是

enter image description here

欲了解更多信息下面是我的CustomTableViewCell和它的约束

enter image description here

enter image description here

我尝试了很多实现了输出,但我无法弄清楚。是否有任何其他方法来实现输出,我准备好遵循任何适合我的要求的方法。任何帮助将非常感激。

+0

看到这个链接它可能会帮助你:http://stackoverflow.com/questions/31823130/how-to-let-a-uiimage-only-stretch-in-a-specific-area –

+0

你可以查看这个项目作为参考:https://github.com/jessesquires/JSQMessagesViewController。可能对你有帮助。该控件以良好的方式拉伸图像。 – Aashish1aug

+0

你需要设置相同的颜色背景来标签,而不是图像。并且给予该标签更严格的等同限制。 – KKRocks

回答

0

你可以通过下面的方法解决问题;

  1. 创建一个UIView。
  2. 设置背景。
  3. 在这个UIView中添加你的标签。

将所有约束条件设置为零。

这可以解决你的问题

-1

做下面的步骤:

  1. 取一个UIView(说,MyView的),并给它的约束和明确的背景色。

    • 顶部尾部到超视图。
    • 导致与关系<=和中等优先级的超级查看。
    • 宽度常数与关系>=和更高的优先级,然后领先。
    • 身高与关系>=
  2. 现在MyView的添加ImageView的带有约束,

    • 领先,尾随顶部,底部,上海华。
  3. 将text_mesage,time的标签添加到myview中,并相应地给出约束条件。

现在运行您的项目并检查。如果还有问题,请告诉我。

1

要解决此问题,您可以将所有imageView约束设置为零。 然后你可以在.xassets文件中添加图像然后使用切片图像。这将在必要时拉伸图像并缩小图像。 Image

切片取决于您。

Image of my storyboard

Output

希望它会帮助你。

+0

有趣的我想实现上述你提到的用户界面,但我没有任何imageview在我的tableviewcell设置约束。我已经将imageview设置为UITableViewCell中标签的背景。任何建议你如何设置imageview? – Madhu

+0

查看答案我更新了。我给了故事板的图像。我在单元格中使用了图像视图,在图像视图中使用了文本级别。 – mumu

+0

请注意,您可以一劳永逸地创建一个“切片”图像(查看UIImage的文档(带有“Stretchable”的部分),这应该可以避免您随时使用UIGraphicsBeginImageContext()来重新创建图像 – Larme

相关问题