UITableViewCell具有可伸缩的背景图像

时间:2012-06-16 10:19:21

标签: iphone objective-c ios xcode

我在UITableView中有单元格,我为单元格上的标签制作了自定义框架,并根据文本调整此框架的大小。

但我的问题是,我如何在气泡(图像)中显示这些文本,因为文本大小增加了气泡图像的大小也会增加。

3 个答案:

答案 0 :(得分:3)

请尝试使用此代码:

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

答案 1 :(得分:3)

试试这个:

在Custom Cell.m文件中:

bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 80, 40)];
[self.contentView addSubview:bgImageView];

messageContentView = [[UITextView alloc] init];
messageContentView.backgroundColor = [UIColor clearColor];
messageContentView.editable = NO;
messageContentView.scrollEnabled = NO;
[messageContentView sizeToFit];
[self.contentView addSubview:messageContentView];

tableView:cellForRowAtindexPath方法中:

static CGFloat padding = 20.0;  
CGSize textSize = {260.0 , 10000.0};
CGSize size = [message sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.width += (padding/2);
UIImage *bgImage = nil;

    if([sender isEqualToString:@"you"])
    {
        bgImage = [[UIImage imageNamed:@"orange.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];

     [cell.messageContentView setFrame: CGRectMake(padding, padding*2, size.width, size.height)];

        [cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/2,cell.messageContentView.frame.origin.y - padding/2,size.width+padding, size.height+padding)];
    }
    else
    {
        bgImage = [[UIImage imageNamed:@"aqua.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];
                [cell.messageContentView setFrame:CGRectMake(320 - size.width - padding,padding*2,size.width, size.height)];
                [cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/2,  cell.messageContentView.frame.origin.y - padding/2,size.width+padding,size.height+padding)];

    }

    cell.bgImageView.image = bgImage;

答案 2 :(得分:2)

您可以使用大写字母拉伸图像,具体取决于您的图像形状

您可以使用此代码

//Your image view
UIImageView *imageView;
//Your image
UIImage *image;

//Left cap is the space you dont wanna stretch on the left side and right side of the image
int leftCap = 20;
//Left cap is the space you dont wanna stretch on the top side and bottom side of the image
int topCap = 20;
//this will only stretch the inner side of the image
imageView.image = [image stretchableImageWithLeftCapWidth:leftCap topCapHeight:topCap];

有关详细信息,请查看此tutorial