iOS 使用 EdgeInsets 对 UIButton 布局

首先先看一个UIButton包含什么东西

UIButton

上图中有三个框,最大的框为 contentView,contentView包含左边的框 imageView 与右边的框为 titleView。UIButton 的默认设置为图片在左,标题在右,两者紧挨并在整个 Button 上居中。为了修改 Button 的图片和标题的布局,使用的方法有修改对齐方式与修改偏移量。

设置 Content 的水平和竖直对齐方式

在 xib 或 storyboard 中,修改对齐方式在 Attributes inspector 的 Control 中,如下图所示:

对齐方式包括靠左(上),居中,靠右(下),拉伸。使用代码方式修改则为:

[button setContentHorizontalAlignment:(UIControlContentHorizontalAlignment)];
[button setContentVerticalAlignment:(UIControlContentVerticalAlignment)];

UIControlContentHorizontalAlignmentUIControlContentVerticalAlignment 包括如下值:

UIControlContentHorizontalAlignmentCenter;//水平方向上居中
UIControlContentHorizontalAlignmentLeft;//水平方向上左对齐
UIControlContentHorizontalAlignmentRight;//水平方向上右对齐
UIControlContentHorizontalAlignmentFill;//水平方向上拉伸,图片可能会被撑大

UIControlContentVerticalAlignmentCenter;//竖直方向上居中
UIControlContentVerticalAlignmentTop;//竖直方向上靠顶部对齐
UIControlContentVerticalAlignmentBottom;//竖直方向上靠底部对齐
UIControlContentVerticalAlignmentFill;//竖直方向上拉伸,图片可能会被撑大

设置 Content, imageView, titleView 的偏移量

在 xib 或 stroyboard 中,修改 content, imageView, titleView 的偏移量在 Size inspector 中,如下图所示:

通过分别调整 Insets 的值,可以在界面中动态查看修改的效果。

那么,这个偏移量指的到底是什么呢?

首先,先来说说这个 content Insets。

在代码中,实现方式为

[button setContentEdgeInsets:UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)];

在第一部分中,我们说了,contentView 包括 imageView 和 titleView。修改 content 的 Insets,会使 imageView 和 titleView 一起移动。当值为正时,在该方向上往中心靠拢,当值为负时,在该方向上往边缘靠拢。如[button setContentEdgeInsets:UIEdgeInsetsMake(100, 0, 0, 0)];,则整个content 在 top 方向上往中心靠拢,即向下偏移。且向下偏移时,图片在超过边界时可能会压缩或拉伸(这个大家可以自行实验)。那么偏移量是 100 吗?根据 NSLog 打印出来的 imageView.frame.origin.y 的差值仅为 50。那么结论是什么呢?

注意!差值为偏移量的一半在某种情况下不成立!原因不明!

注意!差值为偏移量的一半在某种情况下不成立!原因不明!

注意!差值为偏移量的一半在某种情况下不成立!原因不明!

在居中对齐时,button 的 content 最终竖直位置为

content.frame.orgin.y = content.frame.orgin.y + contentEdgeInsets.top/2.0 - contentEdgeInset.bottom/2.0;

其实在 Objective-C 中,并没有直接能够查看 content 的 frame 的值,我是通过 imageView 的frame 来进行推测的。

imageView 和 titleView 的 Insets 设置也同理。

button.imageEdgeInsets = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
button.titleEdgeInsets = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);

不过,imageView 和 titleView 的最终位置需要考虑到之前 contentView 的偏移量。如:button的imageView最终竖直位置为:

imageView.frame.origin.y = imageView.frame.origin.y + contentEdgeInsets.top/2.0 + imageEdgeInsets.top/2.0 - imageEdgeInsets.bottom/2.0 - contentEdgeInsets.bottom/2.0;

在最左对齐时,偏移量就不用除以 2 了,即

content.frame.orgin.y = content.frame.orgin.y + contentEdgeInsets.top - contentEdgeInset.bottom;

图在文右,图在文上,图在文下的实现

有时候,除了默认的图在文左的按钮,我们可能需要实现图在文右,图在文上,图在文下等需求。根据上述 EdgeInsets 的方法,通过计算偏移量,可以实现这些需求。

直接上代码吧。

图在文上:

- (IBAction)imageUpLabelDown:(id)sender {
    [_resetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(-_resetButton.imageView.bounds.size.height, _resetButton.bounds.size.width/2 - _resetButton.imageView.bounds.size.width/2, 0, 0);
    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(_resetButton.titleLabel.bounds.size.height, _resetButton.bounds.size.width/2 - _resetButton.titleLabel.bounds.size.width/2 - _resetButton.imageView.bounds.size.width, 0, 0);
    
//理论上在水平居中环境下下述情况应该也能成立,但是实际效果并不能实现,原因不明。
//    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(-_resetButton.imageView.bounds.size.height, _resetButton.titleLabel.bounds.size.width, 0, 0);
//    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(_resetButton.titleLabel.bounds.size.height, -_resetButton.imageView.bounds.size.width, 0, 0);
}

图在文下:

- (IBAction)imageDownLabelUp:(id)sender {
    [_resetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(_resetButton.imageView.bounds.size.height, _resetButton.bounds.size.width/2 - _resetButton.imageView.bounds.size.width/2, 0, 0);
    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(-_resetButton.titleLabel.bounds.size.height, _resetButton.bounds.size.width/2 - _resetButton.titleLabel.bounds.size.width/2 - _resetButton.imageView.bounds.size.width, 0, 0);
}

图文均居中:

- (IBAction)imageCenterLabelCenter:(id)sender {
    [_resetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(0, _resetButton.bounds.size.width/2 - _resetButton.imageView.bounds.size.width/2, 0, 0);
    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(0, _resetButton.bounds.size.width/2 - _resetButton.titleLabel.bounds.size.width/2 - _resetButton.imageView.bounds.size.width, 0, 0);
}

图在文右:

- (IBAction)imageRightLabelLeft:(id)sender {
    
    [_resetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(0, _resetButton.bounds.size.width/2 - _resetButton.imageView.bounds.size.width/2 + _resetButton.titleLabel.bounds.size.width/2, 0, 0);
    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(0, _resetButton.bounds.size.width/2 - 3 *_resetButton.imageView.bounds.size.width/2 - _resetButton.titleLabel.bounds.size.width/2, 0, 0);
//理论上在水平居中环境下下述情况应该也能成立,但是实际效果并不能实现,原因不明。
//    [_resetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
//    _resetButton.imageEdgeInsets = UIEdgeInsetsMake(0, _resetButton.titleLabel.bounds.size.width*2, 0, 0);
//    _resetButton.titleEdgeInsets = UIEdgeInsetsMake(0, -_resetButton.imageView.bounds.size.width*2, 0, 0);
}

效果如下图所示:

效果图
效果图

写在最后

其实随手一搜就能搜到很多对 EdgeInset 的文章,但是到自己动手实现时,还是遇到了一些问题,(如在居中环境下差值不为偏移量的一半,)因此自己动手实现有助于帮助自己理解 EdgeInset 的问题。随手附上我自己学习时做的 Demo

参考链接

  1. Phelthas 的博客 - UIButton的titleEdgeInsets属性和imageEdgeInsets属性实现图片文字按要求排列
  2. emily_sky 的简书 - iOS 调整UIButton 图片(imageView)与文字(titleLabel)的位置
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,943评论 4 60
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,490评论 18 139
  • 以前知道与客户交流沟通,要达成成交,说话有一定的技巧方式;今天学习了就算在公司汇报工作也大有套路,原来那些实际业务...
    努力学习的清梅阅读 167评论 0 5
  • 1,2017年1月1日 发布有书: (1)2017年誓读52本书,相当于每周共读1本 (2)做个知行合一的实践者 ...
    cissyfriends阅读 187评论 0 0
  • 枚举是为一组相关值定义的一个通用的类型,使我们在代码中能够安全地操作这些值。 如果对C语言比较熟悉,就会知道在C语...
    杨雪丹阅读 562评论 0 2