以前一直对类似聊天气泡背景图片拉伸的设置纠结,不管如何设置UIEdgeInsets属性都不能正常设置,今天对以下几种情况进行了总结,如有需要的同学可进行参考:
项目地址:https://github.com/loverjiao0321/EdgeInsetsDemo
说明:
/**
1、UIEdgeInsets中的值都可以以像素为单位进行设置
2、测量凸起部分距离图片边缘值时,若有存在圆角部分,计算时最好将圆角部分出去,如: - (void)addDialogueLeft;方法中左侧凸起部分为12像素,图片额外含圆角部分8像素,则UIEdgeInsets中left和right值>= (12+8)计算
**/
/**
*添加左侧凸起背景图片
*/
- (void)addDialogueLeft
{
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, dialogueCount * (kDialogueSpace + kDialogueViewHeight), 315, kDialogueViewHeight)];
UIImage *image = [UIImage imageNamed:@"dialogueLeft"];
/*该图片尺寸为354*97
**1、凸起部分右侧距离图片左侧边缘大概为20(包括圆角部分宽度),因此UIEdgeInsets中left和right值>= 20即可.(此值若不合适继续调大)
**2、不涉及上下距离,整体高度为97,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合适继续调小)
*/
UIEdgeInsets insets = UIEdgeInsetsMake(45, 20, 45, 20);
UIImage *insetImage = [image resizableImageWithCapInsets:insets];
bgImageView.image = insetImage;
[self.view addSubview:bgImageView];
dialogueCount++;
}
/**
*添加右侧凸起背景图片
*/
- (void)addDialogueRight
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueRight"];
/*该图片尺寸为354*97
**1、凸起部分左侧距离图片右侧边缘大概为20(包括圆角部分宽度),因此UIEdgeInsets中left和right值>= 20即可.(此值若不合适继续调大)
**2、不涉及上下距离,整体高度为97,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合适继续调小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(45,20,45,20);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}
/**
*添加左上角凸起背景图片
*/
- (void)addDialogueTopLeft
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueTopLeft"];
/*该图片尺寸为343*106
**1、凸起部分右侧距离图片左侧边缘大概为27,因此UIEdgeInsets中left和right值>= 27即可.(此值若不合适继续调大)
**2、凸起部分底部图片高度(除去凸起高度)大概为98,因此UIEdgeInsets中top和bottom值<= 49即可.(此值若不合适继续调小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(40,27,40,27);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}
/**
*添加左下角凸起背景图片
*/
- (void)addDialogueBottomLeft
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueBottomLeft"];
/*该图片尺寸为343*106
**1、凸起部分右侧距离图片左侧边缘大概为23,因此UIEdgeInsets中left和right值>= 23即可.(此值若不合适继续调大)
**2、凸起部分底部图片高度(除去凸起高度)大概为97,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合适继续调小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(40,23,40,23);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}
/**
*添加右上角凸起背景图片
*/
- (void)addDialogueTopRight
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueTopRight"];
/*该图片尺寸为343*106
**1、凸起部分左侧距离图片右侧边缘大概为22,因此UIEdgeInsets中left和right值>= 22即可.(此值若不合适继续调大)
**2、凸起部分底部图片高度(除去凸起高度)大概为98,因此UIEdgeInsets中top和bottom值<= 49即可.(此值若不合适继续调小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(40,22,40,22);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}
/**
*添加右下角凸起背景图片
*/
- (void)addDialogueBottomRight
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueBottomRight"];
/*该图片尺寸为343*106
**1、凸起部分左侧距离图片右侧边缘大概为54,因此UIEdgeInsets中left和right值>= 54即可.(此值若不合适继续调大)
**2、凸起部分底部图片高度(除去凸起高度)大概为96,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合适继续调小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(45,54,45,54);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}