【问题标题】:Resize background image based on UITableViewCell Label height and width根据 UITableViewCell 标签高度和宽度调整背景图片大小
【发布时间】: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 及其约束的更多信息

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

【问题讨论】:

标签: ios objective-c iphone uitableview


【解决方案1】:

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

切片由你决定。

希望对你有帮助。

【讨论】:

  • 有趣的是,我想实现您提到的上述 ui,但我的 tableviewcell 中没有任何 imageview 来设置约束。我已将 imageview 设置为 UITableViewCell 中标签的背景。有什么建议您如何设置图像视图?
  • 查看我更新的答案。我给出了故事板的图像。我在单元格中获取了图像视图,并在图像视图中获取了文本级别。
  • 请注意,您可以一劳永逸地创建“切片”图像(查看 UIImage 的文档(带有“可拉伸”的部分)。这应该避免您一直使用重新创建图像UIGraphicsBeginImageContext().
  • 感谢您的建议。尝试了您的方法,但没有运气。
  • github.com/rubaiyat6370/iOS/blob/master/… 查看这个项目。希望对您有所帮助。 @马杜
【解决方案2】:

您可以通过以下方式解决问题;

  1. 创建一个 UIView。
  2. 为其设置背景。
  3. 在此 UIView 中添加您的标签。

将此视图的所有约束设置为零。

这可以解决你的问题

【讨论】:

    【解决方案3】:

    执行以下步骤:

    1. 获取一个 UIView(比如 myView)并给出它的约束和清除背景颜色。

      • 顶部底部尾随到超级视图。
      • 导致具有关系<= 和中等优先级的superview。
      • 宽度常数与关系 >= 和优先级高于前导。
      • 身高与关系>=
    2. 现在在 myView 中添加带有约束的 ImageView,

      • 前导、尾随顶部、底部到超级视图。
    3. 为 text_mesage、时间添加标签,在 myview 中看到并相应地给出约束。

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

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 2013-12-12
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多