• 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;

    }

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

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

  • 相关阅读:
    今天,我们来聊一聊互联网真的有你所期待的那么好吗?来自一个老码农的碎碎念
    新鲜出炉!阿里Java后端面经,已拿offer!
    面试阿里,字节跳动,美团必被问到的红黑树原来这么简单
    凭借着这份Spring面试题,我拿到了阿里,字节跳动美团的offer!
    深度分析:理解Java中的多态机制,一篇直接帮你掌握!
    gdb调试core dump使用
    665. Non-decreasing Array
    netstat命令详解
    ifconfig命令
    #paragma详解
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5749404.html
Copyright © 2020-2023  润新知