方法一:使用背景圖片拉伸管怠,這里采用一種相對(duì)簡(jiǎn)單的方式。
需要:背景圖片验残。
自定義一個(gè)view,在上面添加一個(gè)imageView巾乳,一個(gè)label您没。然后在獲得label的內(nèi)容時(shí)候調(diào)整label尺寸,同時(shí)適配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
{
? ? //圖片拉伸區(qū)域
? ? 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;
? ? //重點(diǎn) 進(jìn)行圖片拉伸
? ? return [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch];
}
效果如下:
方法二:如果沒(méi)有背景圖片药版,就需要根據(jù)frame自己畫(huà)邊線辑舷。使用貝塞爾曲線。實(shí)現(xiàn)代碼:
????????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自適應(yīng)的高度
看下效果: