UIButton上图片和文字的位置调整
2015-12-15 10:47
337 查看
UIButton 上默认是图片在左文字在右,而大多数情况这样默认的的显示形式都不能满足我们的需求,接下来我就这个问题分享一下我的心得。
默认情况下,不设置的效果,都是居中实现
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 150, 100);
button.backgroundColor = [UIColor yellowColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:@"title" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"tab5"] forState:UIControlStateNormal];
[self.view addSubview:button];
*********************************************************
UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
上面的四个数值是基于原位置而改变的例如:
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 60, 0, 0)];
title 的CGFloat right改变的60 是基于原位置的titleLabel的右边框向左平移60也就是到右边框的距离。
image也是一样,距离原来imageView的左边框向右平移了60。
*********************************************************
[button setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 30)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
上下移动的原理同左右;
***********************************************
* 注意:这4个数值的位移都是基于原来的位置进行移动的例如第 *
* 一个数就是基于原来上边框的位置向下移动,正数向下移动负数 *
* 向上移动;左右同理; *
***********************************************
默认情况下,不设置的效果,都是居中实现
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 150, 100);
button.backgroundColor = [UIColor yellowColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:@"title" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"tab5"] forState:UIControlStateNormal];
[self.view addSubview:button];
*********************************************************
UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
上面的四个数值是基于原位置而改变的例如:
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 60, 0, 0)];
title 的CGFloat right改变的60 是基于原位置的titleLabel的右边框向左平移60也就是到右边框的距离。
image也是一样,距离原来imageView的左边框向右平移了60。
*********************************************************
[button setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 30)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
上下移动的原理同左右;
***********************************************
* 注意:这4个数值的位移都是基于原来的位置进行移动的例如第 *
* 一个数就是基于原来上边框的位置向下移动,正数向下移动负数 *
* 向上移动;左右同理; *
***********************************************
相关文章推荐
- 关于去哪儿网的UI自动化测试脚本
- UISearchDisplayController和UISearchBar
- Uip在STM32平台移植之建立UDP链接
- MiniUI——表格分组
- 让UIView窄斜
- 187. Repeated DNA Sequences (String; Bit)
- pt-query-digest查询日志分析工具
- easyui combotree 异步加载节点 默认值 问题
- 记录错误:'command '/.../sdk/build-tools/23.0.1/aapt'' finished with non-zero exit value 1
- easyUI在可编辑的datagrid中计算两列的值
- Java GUI入门教程
- 应用Druid监控SQL语句的执行情况
- UIViewContentMode各类型效果
- Querying JSON
- ORA-12520: TNS:listener could not find available handler for requested type of server
- 详解iOS开发中UItableview控件的数据刷新功能的实现
- Arduino - 改装车库门,增设手机蓝牙控制功能
- ios UITableview 刷新某一个cell 或 section
- 《UIPageControl 的创建和 自定义和引导页的使用》
- iOS9 新增 UIStackView 官方文档翻译