zoukankan      html  css  js  c++  java
  • 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;

    }

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

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

  • 相关阅读:
    PL/SQL 入门
    Nginx 安装和配置
    MySql 优化方案
    类加载器(ClassLoader)
    动态代理入门
    Servlet 3.0 介绍
    反射加强(一)
    Python(1)—is和==区别
    代码题(10)— 验证二叉搜索树、二叉搜索树的最近公共祖先
    代码题(9)— 二叉树的最大、最小深度、平衡二叉树
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5749404.html
Copyright © 2011-2022 走看看