iOS开发基础10-图片拉伸

一、内边距

UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距

1、contentEdgeInsets

  如果是设置contentEdgeInsets, 会把UIImageView和UIlabel当做一个整体移动

btn.contentEdgeInsets = UIEdgeInsetsMake(30, 0, 0, 0);

对应状态:

 

2、titleEdgeInsets/imageEdgeInsets

  如果是设置titleEdgeInsets/imageEdgeInsets. 那么不会影响到另外一个, 也就是只会改变当前设置的这个控件

    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);

    btn.imageEdgeInsets = UIEdgeInsetsMake(30 ,0 , 0, 0);

 

 

二、图片拉伸

1.iOS5以前

UIButton *btn = [[UIButton alloc] init];

    UIImage *image = [UIImage imageNamed:@"common_button_blue_highlighted"];



    // LeftCapWidth: 左边多少不能拉伸

    // 右边多少不能拉伸 = 控件的宽度 - 左边多少不能拉伸 - 1

    //  right  =  width - leftCapWidth - 1

    // 1 = width - leftCapWidth - right

 

    // topCapHeight: 顶部多少不能拉伸

    // 底部有多少不能拉伸 = 控件的高度 - 顶部多少不能拉伸 - 1

    //  bottom =  height - topCapWidth - 1

    // 1 = height - topCapWidth - bottom

    UIImage *newImage = [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];

2.iOS5开始

    // UIEdgeInsets是告诉系统哪些地方需要受保护, 也就是不可以拉伸

    // resizableImageWithCapInsets默认拉伸方式是平铺

    UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5);

    UIImage *newImage =  [image resizableImageWithCapInsets:insets];

3.iOS6开始

  // resizingMode指定拉伸模式

    // 平铺

    // 拉伸

    UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5);

    UIImage *newImage =  [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

 

    [btn setBackgroundImage:newImage forState:UIControlStateNormal];

    btn.frame = CGRectMake(100, 100, 200, 80);

    [self.view addSubview:btn];

 

posted @ 2015-07-17 23:05  Mr.陳  阅读(3670)  评论(0编辑  收藏  举报