【问题标题】:Stretching bubble image in Chat app在聊天应用程序中拉伸气泡图像
【发布时间】:2017-05-20 22:42:25
【问题描述】:

我正在创建一个聊天应用程序。我必须像其他chatapps 一样根据文本拉伸bubble 图像。我正在使用 AutoLayout 约束。包含气泡增加器的图像视图大小完美。 (我已经给它黄色背景颜色)。气泡图像没有被拉伸。

我添加了这些约束:

为了拉伸图像,我添加了这个:

let myImage =  self.imageNamed("Bubble_Me")
        let new_Image = myImage.stretchableImage(withLeftCapWidth: 15, topCapHeight: 14)
        cell.bubbleImage.image = new_Image

如果有人对此有任何想法,请回复。 提前致谢。

【问题讨论】:

  • 您在 Assets.xcassets 中对图像进行切片了吗?
  • 是的,相同的图像切片适用于其他所有代码都以编程方式实现的代码。我想对自动布局做同样的事情。它不适用于自动布局。

标签: ios uiimage chat image-resizing stretch


【解决方案1】:

首先使用 this 获取文本的大小。

UIFont *font = [UIFont systemFontOfSize:14.0];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                                                forKey:NSFontAttributeName];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:message attributes:attrsDictionary];

    CGRect paragraphRect =
    [attrString boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 20 , CGFLOAT_MAX)
                                 options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                 context:nil];

获取文本框架后,获取文本框架的高度并将高度放入气泡的 imageView 中。把这一切做的事情都放到tableViewCell自定义类里放到-(void)layoutSubviews method.

【讨论】:

    【解决方案2】:

    你应该做的是在 TextView 的背景中设置图像,以便它尽可能多地拉伸 textview

    text.backgroundColor =  UIColor(patternImage: UIImage(named:"bg.png")!)
    

    对于此类问题,您应该始终使用“可调整大小的位图”,即众所周知的 9-patch 图像。

    【讨论】:

      【解决方案3】:

      试试这个@SNarula

      cell.bubbleImage.image = [[UIImage imageNamed:@"Bubble_Me"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20) resizingMode:UIImageResizingModeStretch];
      

      【讨论】:

        【解决方案4】:

        试试这个:

        cell.bubbleImage.image = [[UIImage imageNamed:@"sender.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
        

        【讨论】:

          猜你喜欢
          • 2021-12-03
          • 2022-11-17
          • 1970-01-01
          • 2016-11-14
          • 2021-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多