ios开发聊天气泡实现

NSArray *arr = @[@"啊",@"啊飞",@"啊飞飞",@"啊飞飞起",@"啊飞飞起来啦",@"啊飞飞起来啦啦",@"啊飞飞起来啦啦啦",@"啊飞飞起来啦啦啦啦",@"啊飞飞起来啦啦啦啦啦啦"];

    for (int i=0 ; i< arr.count; i++) {

    NSString *bglabText = arr[i];

    UIImageView *qipaoImg = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100 + 50 *i, [self receiveTextWidthWithString:bglabText] + 30, 35)];

    UIImage *img = [UIImage imageNamed:@"box2"];

    CGFloat scale = [UIScreen mainScreen].scale;

    UIImage *dot9 = [img resizableImageWithCapInsets:(UIEdgeInsets){0/scale,20/scale,5/scale,20/scale} resizingMode:UIImageResizingModeStretch];  //{top/scale,left/scale,bottom/scale,right/scale}  top  left bottom right 表示图片不拉伸的尺寸.

    qipaoImg.image = dot9;

    [self.view addSubview:qipaoImg];

    int xStart = 10;

    UILabel *bglab = [[UILabel alloc] initWithFrame:CGRectMake(xStart, 5, qipaoImg.frame.size.width - xStart*2, qipaoImg.frame.size.height - 15)];

    //bglab.numberOfLines = 0;

        bglab.textAlignment = NSTextAlignmentCenter;

    bglab.text = bglabText;

    bglab.font = [UIFont systemFontOfSize:13];

    [qipaoImg addSubview:bglab];

    }

 

// 得到文字的宽度

-(int)receiveTextWidthWithString:(NSString *)str

{

    CGSize maxWidth = CGSizeMake(MAXFLOAT, 20);

    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};

    CGRect rect = [str boundingRectWithSize:maxWidth options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    int width = (int)rect.size.width;

    return width;

}

 气泡图片, 可以自行拖出来使用

补充, 特别短的图片适合文字少的,   长点的图片适合文字多的,具体适配得具体调节.这里只是给出了方法, 具体使用还得调 啊. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值