UIImage部分拉伸——stretchableImageWithLeftCapWidth的使用
2016-03-10 14:10
661 查看
简介:有时候我们只是想把图片部分拉伸,而不是全部拉伸,那么就要用到下面这个函数,并附上实例验证
效果图
横向拉伸:
效果图:
练习二:最近做到一个聊天框的联系,对文字背景图片的拉伸应用,左边的小三角我不希望拉伸,拉伸影响美观,只拉伸右边方框部分,那么调用此函数就可以实现,这个较为常用。
效果图:
<code class="hljs" objectivec=""> - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight; //leftCapWidth:左边不拉伸区域 //topCapHeight:上面不拉伸区域</code>``` 对距离leftCapWidth的1竖排像素,和具体topCapHeight的1横排像素进行拉伸,其它像素不拉伸 练习一:理解拉伸点,左边和上面分开设置,从效果图来理解。
self.view.backgroundColor = [UIColor darkGrayColor];
//不设置拉伸点,直接设置 UIImage *image1 = [UIImage imageNamed:@QQ]; UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 170, 170)]; imageView1.image = image1; [self.view addSubview:imageView1]; //设置拉伸点,对左边和上面分开设置,理解其拉伸效果 UIImage *image2 = [UIImage imageNamed:@QQ]; image2 = [image2 stretchableImageWithLeftCapWidth:0 topCapHeight:image2.size.height*0.5]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(190, 100, 170, 170)]; imageView2.image = image2; [self.view addSubview:imageView2];</code>
效果图
横向拉伸:
<code avrasm="" class="hljs"> image2 = [image2 stretchableImageWithLeftCapWidth:image2.size.width*0.5 topCapHeight:0]; </code>
效果图:
练习二:最近做到一个聊天框的联系,对文字背景图片的拉伸应用,左边的小三角我不希望拉伸,拉伸影响美观,只拉伸右边方框部分,那么调用此函数就可以实现,这个较为常用。
<code class="hljs" objectivec=""> //不设置拉伸点,直接设置 UIImage *image1 = [UIImage imageNamed:@chat]; UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 80)]; imageView1.image = image1; [self.view addSubview:imageView1]; //设置拉伸点 UIImage *image2 = [UIImage imageNamed:@chat]; image2 = [image2 stretchableImageWithLeftCapWidth:image2.size.width*0.5 topCapHeight:image2.size.width*0.8]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 80)]; imageView2.image = image2; [self.view addSubview:imageView2];</code>
效果图:
相关文章推荐
- java for循环跳出(break)/结束本次(continue)多重循环
- EasyTouch与UGUI的结合使用
- 快速排序实现(QuickSort)
- 使UITableViewCell之间的cell不显示分割线
- easyui tree的简单使用
- 自定义View--------Android UI模板设计
- iOS UILabel自适应高度宽度
- 让UITextField只能输入数字以及限制长度
- DUILIB项目中自定义快捷键功能的实现方法
- 关于UINavigationController — navigationBar
- UIKeyboardType
- 给UIImageView添加点击事件
- PAT-Pop Sequence (25)
- #error : Please use the /MD switch for _AFXDLL builds
- Android stduio 插件推荐(一)
- @RequestBody
- uilib 属性表全
- cf B. Guess That Car!
- UIScrollView的delaysContentTouches与canCencelContentTouches属性
- UITableView上button的点击事件(一)