简体   繁体   English

使用resizableImageWithCapInsets将拉伸的UIImage设置为UILabel的背景

[英]Setting stretched UIImage as background for UILabel using resizableImageWithCapInsets

I have a chat application and I want to implement the chat bubbles as the background for the UILabel . 我有一个聊天应用程序,我想实现聊天气泡作为UILabel的背景。 This is what I'm trying to implement: 这就是我要实现的:

在此处输入图片说明

I tried using : 我尝试使用:

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

which results in the image being stretched equally like so: 这会导致图像被同样拉伸,如下所示: 在此处输入图片说明

So I tried using the resizableImageWithCapInsets and setting the image using colorWithPatternImage . 所以我尝试使用resizableImageWithCapInsets并使用colorWithPatternImage设置图像。

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

But all it does is repeat the images as the background like so: 但是它所做的就是将图像重复为背景,如下所示:

在此处输入图片说明

How do I stretch only the middle part of the image? 如何仅拉伸图像的中间部分?

Or is it not possible using a UILabel ? 还是无法使用UILabel

For chating using the custom classes of PTSMessagingCell Here you can customize your images and other controls. 使用PTSMessagingCell的自定义类进行聊天时,您可以在此处自定义图像和其他控件。

Catch the sample code PTSMessagingCell-master 捕获示例代码PTSMessagingCell-master

try this. 尝试这个。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UIImageView *balloonView;
UILabel *label;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;       

    balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
    balloonView.tag = 1;

    label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.tag = 2;
    label.numberOfLines = 0;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.font = [UIFont systemFontOfSize:14.0];

    UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
    message.tag = 0;
    [message addSubview:balloonView];
    [message addSubview:label];
    [cell.contentView addSubview:message];

    [balloonView release];
    [label release];
    [message release];
}
else
{
    balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:1];
    label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2];
}

NSString *text = [messages objectAtIndex:indexPath.row];
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];

UIImage *balloon;

if(indexPath.row % 2 == 0)
{
    balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
    balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
}
else
{
    balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15);
    balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(16, 8, size.width + 5, size.height);
}

balloonView.image = balloon;
label.text = text;

return cell;
      }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *body = [messages objectAtIndex:indexPath.row];
    CGSize size = [body sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
    return size.height + 15;
}

i hope this code is useful for you. 我希望这段代码对您有用。

variable newSize must be equal to size of label. 变量newSize必须等于标签的大小。

and replace this 并替换这个

cell.answer.backgroundColor = [UIColor newImage];

with

cell.answer.backgroundColor = [UIColor colorWithPatternImage:newImage];

Let me know if it works :) 让我知道它是否有效:)

Reference Link : 参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM