您的位置:首页 > 产品设计 > UI/UE

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个数值的位移都是基于原来的位置进行移动的例如第 *

* 一个数就是基于原来上边框的位置向下移动,正数向下移动负数 *

* 向上移动;左右同理; *

***********************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: