iOS 聊天气泡

方法一:使用背景图片拉伸,这里采用一种相对简单的方式。

需要:背景图片。

自定义一个view,在上面添加一个imageView,一个label。然后在获得label的内容时候调整label尺寸,同时适配imageView的尺寸。然后拉伸背景图片填充iamgeView。代码如下:

#import "PopView.h"

@interface PopView ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UILabel *textLable;

@end

@implementation PopView

-(instancetype)init

{

    if (self = [super init])

    {

        self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];

        [self addSubview:self.imageView];

        self.textLable = [[UILabel alloc]init];

        [self addSubview:self.textLable];

        self.textLable.font = [UIFont systemFontOfSize:16];

        self.textLable.numberOfLines = 0;

    }

    return self;

}

- (void)setContentText:(NSString *)text

{

    self.textLable.text = text;

    CGSize maxSize = CGSizeMake(200, 990);

    CGSize size = [self.textLable sizeThatFits:maxSize];

    self.textLable.frame = CGRectMake(0, 0, size.width, size.height);

    self.imageView.frame = CGRectMake(-10, -10, size.width + 25 , size.height + 20);

    UIImage *image = [UIImage imageNamed:@"bg_image"];

    self.imageView.image = [self resizableImage:image];

}

- (UIImage*)resizableImage:(UIImage *)image

{

    //图片拉伸区域

    CGFloat top = image.size.height - 8;

    CGFloat left = image.size.width / 2 - 2;

    CGFloat right = image.size.width / 2 + 2;

    CGFloat bottom = image.size.height - 4;

    //重点 进行图片拉伸

    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch];

}

效果如下:

方法二:如果没有背景图片,就需要根据frame自己画边线。使用贝塞尔曲线。实现代码:

        CGFloatleftSpace = 69.0+10.0;

        CGFloatradius =10.0;

        UIBezierPath *path = [UIBezierPath bezierPath];

        [pathmoveToPoint:CGPointMake(leftSpace,0)];

        [pathaddLineToPoint:CGPointMake(LeaveMessageTextWidth+ leftSpace - radius,0)];

        [pathaddArcWithCenter:CGPointMake(LeaveMessageTextWidth + leftSpace, radius) radius:radius startAngle:- M_PI_2 endAngle:0 clockwise:YES];//右上

        [pathaddLineToPoint:CGPointMake(LeaveMessageTextWidth+ leftSpace + radius,self.replayHeight- radius)];

        [pathaddArcWithCenter:CGPointMake(LeaveMessageTextWidth + leftSpace, self.replayHeight - radius) radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];//右下

        [pathaddLineToPoint:CGPointMake(leftSpace,self.replayHeight)];

        [pathaddArcWithCenter:CGPointMake(leftSpace, self.replayHeight - radius) radius:radius startAngle:-M_PI*3/2.0 endAngle:-M_PI clockwise:YES];//左下

        [pathaddLineToPoint:CGPointMake(leftSpace -10.0,30.0)];

        [pathaddLineToPoint:CGPointMake(leftSpace -20.0,25.0)];

        [pathaddLineToPoint:CGPointMake(leftSpace -10.0,20.0)];

        [pathaddLineToPoint:CGPointMake(leftSpace-10.0,20.0)];

        [pathaddLineToPoint:CGPointMake(leftSpace - radius, radius)];

        [pathaddArcWithCenter:CGPointMake(leftSpace, radius) radius:radius startAngle:-M_PI endAngle:-M_PI_2 clockwise:YES];//左上


        CAShapeLayer *layer = [CAShapeLayer layer];

        layer.fillColor = [UIColor clearColor].CGColor;

        layer.lineWidth=0.5;

        layer.strokeColor=RGB(127,127,127).CGColor;

        layer.path= path.CGPath;

        [self.layeraddSublayer:layer];

//replayHeight就是自己label自适应的高度

看下效果:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 5,275评论 0 3
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,826评论 1 6
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,372评论 1 14
  • 我就是我,啤酒过敏。
    徐大哥阅读 2,338评论 2 1
  • 这是怎样的一个团队? 是纯心性成长的团队, 所说所做不为别的 只为了彼此的心灵成长 与生命状态变得更好 这里对您没...
    玲旭阅读 2,507评论 0 0

友情链接更多精彩内容