【问题标题】:Setting stretched UIImage as background for UILabel using resizableImageWithCapInsets使用 resizableImageWithCapInsets 将拉伸的 UIImage 设置为 UILabel 的背景
【发布时间】:2014-04-03 10:14:46
【问题描述】:

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

我尝试使用:

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

这会导致图像被同样拉伸,如下所示:

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

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

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

如何仅拉伸图像的中间部分?

还是不能使用UILabel?

【问题讨论】:

  • 我有一个用于聊天的演示,非常简单,所以请给我您的电子邮件 ID,我将发送该演示并检查该演示。
  • 我认为停止使用 backgroundColor 并简单地使用 UIImageView,它将具有与标签相同的框架(或每边稍大一点)并将其放在标签下。这样,您还可以将标签从气泡的侧面偏移。)如果您确实设法将其放置为背景 - 您将永远无法从侧面偏移文本。
  • @DarshanKunjadiya 请通过 unnikrishnan.anil@gmail.com 发送给我
  • @GuntisTreulands 我会尝试使用 UIImageView。
  • 好的,我现在发送。

标签: ios iphone objective-c uiimage uilabel


【解决方案1】:

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

获取示例代码PTSMessagingCell-master

【讨论】:

    【解决方案2】:

    试试这个。

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

    我希望这段代码对你有用。

    【讨论】:

    • 效果很好。我也看到了这个。但我想使用 Storyboards 来完成,因为我还需要向单元格添加其他内容。这样更容易。以编程方式,这需要时间。
    【解决方案3】:

    变量 newSize 必须等于标签的大小。

    并替换它

    cell.answer.backgroundColor = [UIColor newImage];
    

    与

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

    让我知道它是否有效:)

    参考Link:

    【讨论】:

    • 什么是newSize?是 uilabel frame.size 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2012-01-27
    • 2012-01-02
    相关资源
    最近更新 更多