ios - 使用 resizableImageWithCapInsets 将拉伸(stretch)的 UIImage 设置为 UILabel 的背景

标签 ios iphone objective-c uiimage uilabel

我有一个聊天应用程序,我想将聊天气泡实现为 UILabel 的背景。这就是我要实现的:

enter image description here

我尝试使用:

CGFloat scale = [[UIScreen mainScreen]scale]; 
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
[img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这导致图像像这样被拉伸(stretch): enter image description here

所以我尝试使用 resizableImageWithCapInsets 并使用 colorWithPatternImage 设置图像。

UIImage *newImage = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
cell.answer.backgroundColor = [UIColor newImage];

但它所做的只是像这样重复图像作为背景:

enter image description here

如何只拉伸(stretch)图片的中间部分?

或者不能使用 UILabel 吗?

最佳答案

使用 PTSMessagingCell 的自定义类进行聊天在这里您可以自定义图像和其他控件。

抓取示例代码PTSMessagingCell-master

关于ios - 使用 resizableImageWithCapInsets 将拉伸(stretch)的 UIImage 设置为 UILabel 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22834500/

相关文章:

iphone - iOS:使用多个库(xyz.a)构建XYZ.framework吗?

ios - 使用 pushwoosh sdk 将应用上传到应用商店的问题

ios - 在 iOS 上使用 TestFlight 进行应用内更新

ios - 设备界面方向

objective-c - NSUserDefaults不更改默认值

ios - 没有 arm64 的应用商店提交

ios - Tilemap 在横向时改变大小

iphone - iPhone 内存监视器是什么?

ios - 如何让用户强制更新可用的应用程序的新版本

ios - 导航栏颜色不会变回来? IOS 7