圖片拉伸,多用於聊天中的氣泡,隨著字體的增多而變高變寬。但是要保證邊角的不變,只拉伸中間的部分。用到2種方法:
1、(UIImage *)stretchableImageWithLeftCapWidth:(NSInterger) topCapHeight:(NSinterger)
這種方法只適用於ios5以下的版本。它只無限拉伸離最左邊像素(第一個參數值) +1和離最上邊的像素的值+1(第二個參數值)。
2、(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)
//方法介紹
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode
//參數結構
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
//用法示例
UIImageView *ImageView=[[UIImageView alloc]init];
[ ImageView setFrame:CGRectMake(126.0, 80.2, 30.0, 20.0)];
UIEdgeInsets ed = {0.0f, 10.0f, 0.0f, 10.0f};
[ImageView setImage:[[UIImage imageNamed:@"xxx.png"]resizableImageWithCapInsets:ed]];
self.view addSubview:ImageView ];
//以上左10.0,右10.0,是左邊10像素以內右邊10像素以內不拉伸,中間拉伸。如果上下左右都有參數,就說明九宮格的四個角不拉伸,其他都拉伸