让你的Button任性起来~~~

奋斗的七月

UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中title和image的布局。利用这两个属性就可以轻松的设置一个图片一个文本这样的布局。
但是先要对着两个属性了解清楚

UIButton *btnCeShi = [[UIButton alloc]initWithFrame:CGRectMake(20, 30, 80, 80)];
[self.view addSubview:btnCeShi];
btnCeShi.backgroundColor = [UIColor redColor];
[btnCeShi setTitle:@"测试" forState:UIControlStateNormal];
[btnCeShi setImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
奋斗的七月

这是一个基本的button,默认情况下,图片在左,文字在右,垂直居中显示,如上图。

只设置image或title其中一项时,默认是正常居中显示的,但我们同时设置image和title时,则会出现上图的显示,这是为什么呢?那就是当我们同时添加image和title时,image默认会向左偏移button的titleLabel的宽度,而title会向右偏移image的宽度,既然明白了原理,我们就可以设置偏移量来达到我们想要的任何效果。

默认情况下

button.titleEdgeInsets = UIEdgeInsetsZero;
button.imageEdgeInsets = UIEdgeInsetsZero;

现在我们就可以任意的修改button的布局

1. 设置图片和文字都是居中的布局
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, 0, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size为0,用上面这样设置有问题,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.intrinsicContentSize.width);
奋斗的七月
2. 设置图片在上,文字在下,水平居中显示
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size为0,用上面这样设置有问题,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height, 0, 0, -button.titleLabel.intrinsicContentSize.width);
奋斗的七月

如果觉得图片和文字离的太近了,稍微分开一点:

CGFloat offset = 40.0f;
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height-offset/2, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height-offset/2, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size为0,用上面这样设置有问题,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -button.titleLabel.intrinsicContentSize.width);
奋斗的七月
2. 设置文字左对齐,图片右对齐
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width - button.frame.size.width + button.titleLabel.intrinsicContentSize.width, 0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.frame.size.width - button.frame.size.width + button.imageView.frame.size.width);
奋斗的七月
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,535评论 0 17
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,805评论 1 92
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 注意:更新iOS 9.3, and Swift 2.2- 2016年4月2日 原文https://www.rayw...
    Tippi阅读 4,383评论 0 2
  • 你倚在船头听闻鱼儿的絮语,稀疏的灯火在远处隐隐约约地泛起。你悄然隐匿在朦胧的月光里,月光亦装进了你枕边的梦。 你如...
    寒竹墨轩阅读 164评论 0 1