方法一:使用背景图片拉伸,这里采用一种相对简单的方式。
需要:背景图片。
自定义一个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自适应的高度
作者:回传妙手 链接:https://www.jianshu.com/p/4ce70da2b5ee 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。