? ? ? ?以前一直對類似聊天氣泡背景圖片拉伸的設(shè)置糾結(jié),不管如何設(shè)置UIEdgeInsets屬性都不能正常設(shè)置犁罩,今天對以下幾種情況進行了總結(jié)齐蔽,如有需要的同學可進行參考:
項目地址:https://github.com/loverjiao0321/EdgeInsetsDemo
說明:
/**
1、UIEdgeInsets中的值都可以以像素為單位進行設(shè)置
2床估、測量凸起部分距離圖片邊緣值時含滴,若有存在圓角部分,計算時最好將圓角部分出去丐巫,如: - (void)addDialogueLeft;方法中左側(cè)凸起部分為12像素谈况,圖片額外含圓角部分8像素勺美,則UIEdgeInsets中l(wèi)eft和right值>= (12+8)計算
**/
/**
*添加左側(cè)凸起背景圖片
*/
- (void)addDialogueLeft
{
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, dialogueCount * (kDialogueSpace + kDialogueViewHeight), 315, kDialogueViewHeight)];
UIImage *image = [UIImage imageNamed:@"dialogueLeft"];
/*該圖片尺寸為354*97
**1、凸起部分右側(cè)距離圖片左側(cè)邊緣大概為20(包括圓角部分寬度)碑韵,因此UIEdgeInsets中l(wèi)eft和right值>= 20即可.(此值若不合適繼續(xù)調(diào)大)
**2赡茸、不涉及上下距離,整體高度為97泼诱,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合適繼續(xù)調(diào)小)
*/
UIEdgeInsets insets = UIEdgeInsetsMake(45, 20, 45, 20);
UIImage *insetImage = [image resizableImageWithCapInsets:insets];
bgImageView.image = insetImage;
[self.view addSubview:bgImageView];
dialogueCount++;
}
/**
*添加右側(cè)凸起背景圖片
*/
- (void)addDialogueRight
{
UIImageView*bgImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(30,dialogueCount* (kDialogueSpace+kDialogueViewHeight),315,kDialogueViewHeight)];
UIImage*image = [UIImageimageNamed:@"dialogueRight"];
/*該圖片尺寸為354*97
**1坛掠、凸起部分左側(cè)距離圖片右側(cè)邊緣大概為20(包括圓角部分寬度),因此UIEdgeInsets中l(wèi)eft和right值>= 20即可.(此值若不合適繼續(xù)調(diào)大)
**2治筒、不涉及上下距離屉栓,整體高度為97,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合適繼續(xù)調(diào)小)
*/
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耸袜、凸起部分右側(cè)距離圖片左側(cè)邊緣大概為27友多,因此UIEdgeInsets中l(wèi)eft和right值>= 27即可.(此值若不合適繼續(xù)調(diào)大)
**2、凸起部分底部圖片高度(除去凸起高度)大概為98堤框,因此UIEdgeInsets中top和bottom值<= 49即可.(此值若不合適繼續(xù)調(diào)小)
*/
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域滥、凸起部分右側(cè)距離圖片左側(cè)邊緣大概為23,因此UIEdgeInsets中l(wèi)eft和right值>= 23即可.(此值若不合適繼續(xù)調(diào)大)
**2蜈抓、凸起部分底部圖片高度(除去凸起高度)大概為97启绰,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合適繼續(xù)調(diào)小)
*/
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、凸起部分左側(cè)距離圖片右側(cè)邊緣大概為22沟使,因此UIEdgeInsets中l(wèi)eft和right值>= 22即可.(此值若不合適繼續(xù)調(diào)大)
**2委可、凸起部分底部圖片高度(除去凸起高度)大概為98,因此UIEdgeInsets中top和bottom值<= 49即可.(此值若不合適繼續(xù)調(diào)小)
*/
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腊嗡、凸起部分左側(cè)距離圖片右側(cè)邊緣大概為54着倾,因此UIEdgeInsets中l(wèi)eft和right值>= 54即可.(此值若不合適繼續(xù)調(diào)大)
**2、凸起部分底部圖片高度(除去凸起高度)大概為96燕少,因此UIEdgeInsets中top和bottom值<= 48即可.(此值若不合適繼續(xù)調(diào)小)
*/
UIEdgeInsetsinsets =UIEdgeInsetsMake(45,54,45,54);
UIImage*insetImage = [imageresizableImageWithCapInsets:insets];
bgImageView.image= insetImage;
[self.viewaddSubview:bgImageView];
}