penn-wang
一位老和尚,他身边聚拢着一帮虔诚的弟子。这一天,他嘱咐弟子每人去南山打一担柴回来。弟子们匆匆行至离山不远的河边,人人目瞪口呆。只见洪水从山上奔泻而下,无论如何也休想渡河打柴了。无功而返,弟子们都有些垂头丧气。唯独一个小和尚与师傅坦然相对。师傅问其故,小和尚从怀中掏出一个苹果,递给师傅说,过不了河,打不了柴,见河边有棵苹果树,我就顺手把树上唯一的一个苹果摘来了。后来,这位小和尚成了师傅的衣钵传人。

原始图片:

拉伸后图片:

具体代码,很简单:

    UIImage *bgImage = [[UIImage imageNamed:@"search_edit_bg.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:0];
//    UIImage *tempImage = [[UIImage imageNamed:@"remind_tohome_btn.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:0];
    UIView *theView = [[UIView alloc] initWithFrame:frame];
    theView.backgroundColor = [UIColor clearColor];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:theView.bounds];
    bgImageView.image = bgImage;
    [theView addSubview:bgImageView];
    [bgImageView release];

从第二个像素,依次复制向后拉伸。

另外:textfiled,垂直居中:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

ok!

 

下面是网上copy的一段新的方法

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像素以内不拉伸,中间拉伸。如果上下左右都有参数,就说明九宫格的四个角不拉伸,其他都拉伸

posted on 2013-01-16 15:25  penn-wang  阅读(427)  评论(0编辑  收藏  举报