0

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

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

4

3 回答 3

3

为您使用的图像尝试此代码:

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
于 2012-06-16T10:24:59.373 回答
3

试试这个:

在自定义 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;
于 2012-06-16T11:01:04.987 回答
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];

有关更多信息,请查看本教程

于 2012-06-16T10:21:44.060 回答