转:http://www.cnblogs.com/huichun/p/3419596.html

uiButton控件上自带了一个uiLabel类型的子控件和一个uiImageView类型的子控件,如果可以正确使用他们的edgeInsets属性,就能把button设置成我们想要的样子。

  关于titleEdgeInsets,苹果文档的解释是:The inset or outset margins for the rectangle around the button’s title text,而且imageEdgeInsets也是类似,都没有讲怎么设。事实上,这两个东西是有联系的,常常会造成困惑:我只设了其中一个的edgeInsets,为什么button上的图片和文字布局都变了?

  这里是一个同事花一个下午的时间,专门写一段button的代码,分析数据,总结出来的小规律,并不权威,但是挺好用的,总结出来分享一下。

默认情况下,imageEdgeInsets和titleEdgeInsets都是0。先不考虑height,

  if (button.width小于imageView上image的width){图像会被压缩,文字不显示}

if (button.width < imageView.width + label.width){图像正常显示,文字显示不全}

if (button.width >= imageView.width + label.width){图像和文字都居中显示,imageView在左,label在右,中间没有空隙}

  实际app应用中,通常会已知如下参数,布局button

  button的width:BUTTON_WIDTH

  button上控件imageView的的图片为image

  label上的文字为:@“这是一个测试”

  为了不看着头疼,不写那么多的常量了,以具体的数字来举例吧,我们想让imageView在前,label在后,居中显示,imageView在button上离左边界至少为距离10,label离button右边界为距离为至少为10,imageView和label之间的距离为5,代码可以如下写:

  NSString *title = @"这是一个测试";

    [button setTitle:title forState:UIControlStateNormal];

    [button setImage:image forState:UIControlStateNormal];

  CGSize strSize = [title sizeWithFont:button.titleLabel.font];

   CGFloat totalLen = strSize.width + 5 + image.size.width;

  CGFloat edgeLen = (TAGS_BUTTON_WIDTH - totalLen) / 2;

   if (edgeLen < 10) {

  edgeLen = 10;

  }

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, edgeLen, 0, edgeLen + 5)];

   [button setTitleEdgeInsets:UIEdgeInsetsMake(0, edgeLen + 5, 0, edgeLen)];

设置edgeInsets要始终记住的一个原则是:将label和imageView看成一个整体,imageView在前,label在后,中间没有空隙。。这段代码中,设置imageEdgeInsets时,imageView与左边距离为计算好的edgeLen,右边距是按照button的默认布局,label的右边与button右边的距离,就是label实际的右边应当与button右边的距离再向左移动5(实际中imageView与label有间距5,默认布局下可没有这个5,得把这个5让出来),就是edgeLen + 5。设置titleEdgeInset时,label与右边为计算好的edgeLen,想象imageView还在label的左边,与label没有空隙的话,那这个整体与左边的距离应该是多少呢?就是edgeLen+5,把间隙的5让出来嘛。

   我们再想一个稍复杂的情况:如果label在左,imageView在右,imageView在button上离右边界为固定值10,label离button左边界也为固定值10,应该怎么设呢?可以如下写代码:

  NSString *title = @"这是一个测试";

    [button setTitle:title forState:UIControlStateNormal];

    [button setImage:image forState:UIControlStateNormal];

  CGSize strSize = [title sizeWithFont:button.titleLabel.font];

   [button setImageEdgeInsets:UIEdgeInsetsMake(0, BUTTON_WIDTH - 10 - image.size.width, 0, (10 - strSize.width))];

  CGFloat titleRightInset = BUTTON_WIDTH - 10 - strSize.width;

  if (titleRightInset < 10 + image.size.width) {

  titleRightInset = 10 + image.size.width;

  }

  [button setTitleEdgeInsets:UIEdgeInsetsMake(0, (10 - image.size.width), 0, titleRightInset)];

      解释这段代码之前再强调一下UIButton控件的默认布局:imageView在左,label在右,中间没有空隙。imageView的左侧与button的左边界距离为button的width,去掉右侧留出的10,再去掉imageView的width,想像imageView后面还接着有一个label,那么label的右侧与button的右边界距离为10 - strSize.width,所以button的imageEdgeInsets属性就如上面代码的设置值了。再看label,它的右侧与button右边界的距离为button的width,去掉左侧留出的10,再去掉label的width,为保证label后面能放下一个图片,图片后面还有10的空白,故对titleRightInset做了如上的一些调整。想象label的左侧还有一个imageView,那么这个整体离button左边界的距离为10 - image.size.width。

    以上只考虑了width方向,height方向与width是独立的,比width更容易一些。

    设button的height:BUTTON_HEIGHT,如果imageView在上,与button上边界距离为10,label在下,与button下边界距离为10,可写如下代码。

  NSString *title = @"这是一个测试";

    [button setTitle:title forState:UIControlStateNormal];

    [button setImage:image forState:UIControlStateNormal];

   [button setImageEdgeInsets:UIEdgeInsetsMake(10, 0, BUTTON_HEIGHT - 10 - image.size.height , 0)];

  [button setTitleEdgeInsets:UIEdgeInsetsMake(BUTTON_HEIGHT - 10 - button.titleLabel.frame.size.height, 0, 10, 0)];

   可以看到height方向上,imageView与label独立变化,不用考虑彼此。

UIButton的titleEdgeInsets和imageEdgeInsets属性的更多相关文章

  1. UIButton的titleEdgeInsets属性和imageEdgeInsets属性实现图片文字按要求排列

    button可以设置 titleEdgeInsets属性和 imageEdgeInsets属性来调整其image和label相对位置,具体参考http://stackoverflow.com/ques ...

  2. iOS:UIView、UIControl、UIButton、UILabel简单的属性和方法常识

    常见属性和方法 一 .UIVIew 常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点 (以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸 ...

  3. UIButton中的三个UIEdgeInsets属性

    接着昨天的 UIButton中的三个UIEdgeInsets属性 ,今天我们具体谈谈UIButton的contentEdgeInsets.titleEdgeInsets.imageEdgeInsets ...

  4. UIButton的imageEdgeInsets 和 titleEdgeInsets

    我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets ...

  5. IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...

  6. UIkit框架之UIbutton的使用

    1.UIbutton的继承关系:UIcontroller:UIview:UIresponder:NSObject: 2.添加按钮的步骤: (1)创建按钮的时候首先设置类型 (2)添加标题或者图片,设置 ...

  7. UIButton 解析

    IOS之按钮控件--Button全解析及使用 转载自:forget  IOS开发中伴随我们始终的 最常用的几个空间之一 -- UIButton 按钮,对于button今天在此做一些浅析,并介绍下主流用 ...

  8. Object-C知识点 (二) 控件的实用属性

    开发过程中的组件不常用但是很实用的属性!!!!!! p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Menlo; color: #78492a ...

  9. [iOS]详解调整UIButton的title和image的位置

    UIButton的默认布局是:title在右,image在左; 很多时候我们需要的是title在左边,或者title在下面,这时就需要调整UIButton的TitleLabel和ImageView的位 ...

随机推荐

  1. (转)对Oracle导出文件错误和DMP文件结构的分析,EXP-00008: 遇到 ORACLE 错误 904 ORA-00904: "MAXSIZE": invalid identifier

    EXP-00008: 遇到 ORACLE 错误 904 ORA-00904: "MAXSIZE": invalid identifier 原因:oracle版本不一样 执行 C:/ ...

  2. WINDOWS的NTP配置

    将下面内容复制到记事本,保存成ntp.bat net stop w32Time REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeP ...

  3. Fiddler怎么对IPhone手机的数据进行抓包分析

    http://www.cr173.com/html/20064_1.html Fiddler绝对称得上是"抓包神器", Fiddler不但能截获各种浏览器发出的HTTP请求, 也可 ...

  4. 使用jQuery.form插件,实现完美的表单异步提交

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...

  5. 【java】:通用接口

    电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID&type=1 用例 ps:商品ID这么获取:http://item.jd ...

  6. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  7. MacOS 10.8更新SVN到1.8.4的问题和解决方法

    因为要导入以前的项目,但以前项目里内含有的svn信息,所以xcode默认安装的svn1.6是无法删除svn信息,据说需要svn1.7才能清除掉svn信息.所以必须要升级svn的版本. 我在网上找了各种 ...

  8. android firmware 利用UDP socket发送Magic Packet--python版本

    android firmware 利用UDP socket发送Magic Packet--python版本 #!/usr/bin/python import sys, time from struct ...

  9. Sql语句,先查询再插入一条语句完成。

    if ( (select COUNT(*) from Hr where 考勤号码 = '149' and 日期时间 = '2015/7/3 12:00:26') = 0 )INSERT  INTO [ ...

  10. 基于JavaScript实现表单密码的隐藏和显示出来

    转载:http://www.jb51.net/article/80326.htm 主要代码:<input type="password" name="pass&qu ...