转: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. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  2. c#输出、输入练习

    //输出 Console.WriteLine("这是一行文字");  自动回车的. Console.Write("Hello world");  不带回车的. ...

  3. [原]iOS自带社会化分享框架——Social.framework

    http://m.blog.csdn.net/blog/u012189584/43236379 Social.framework支持的分享平台(打开手机上的“设置”即可看到)(如Twitter.fac ...

  4. c# 通过关键字查询

    1:首先需要在前端显示界面View视图中添加查询按钮: <div> <div>@Html.NopLabelFor(model => model.IndividualNam ...

  5. CALayer图层的基本介绍

    掌握 ● CALayer的基本属性 ● CALayer和UIView的关系 ● position和anchorPoint的作⽤用 CALayer ● 在iOS中,你能看得见摸得着的东西基本上都是UIV ...

  6. 气象API(2)

    中华万年历: http://wthrcdn.etouch.cn/weather_mini?city=北京通过城市名字获得天气数据,json数据http://wthrcdn.etouch.cn/weat ...

  7. 使用wxPython WebView浏览器版本问题

    使用CodeMirror和wxPyhton的WebView创建嵌入客户端的本地代码编辑工具. 版本为wxPython 3.0,CodeMirror 支持的浏览器IE8或以上. wxPython提供了H ...

  8. linux下使用SSL代理(SSLedge)

    refer to: https://eurekavpt.com/page/ssledge-on-linux 启动非常简单./ssledge-term-x64 -f config -D 其中的confi ...

  9. 【整理】--VC 编译整理

    .h用于编译阶段的审核,如在math.h中有函数声明:int abs(int);但是在使用中写为#include <math.h>...abs(3,5);编译器阶段就会检测出错误. .dl ...

  10. 实验五 含有控制信号的计数器VHDL设计

    一.实验目的 学习计数器的设计.仿真和硬件测试,进一步熟悉VHDL设计技术. 二.实验仪器与器材 计算机1台,GW48-PK2S实验箱1台,QuartusⅡ6.0 1套. 三.实验 1. 基本命题 在 ...