特常用的属性说明:
UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,给UIButton设置了title和image后,图片在左边,文本在图片右边显示,它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。
1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。
2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。
3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变水平对齐方式。
想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。
一、属性
-
contentEdgeInsets: default is UIEdgeInsetsZero.设置内容四边距,默认边距为0
-
titleEdgeInsets: default is UIEdgeInsetsZero,文字内边距。默认边距为0.
-
reversesTitleShadowWhenHighlighted: default is NO. if YES, shadow reverses to shift between engrave and emboss appearance。默认状态是 NO,按钮高亮时,阴影出现在相反位置。
-
imageEdgeInsets: default is UIEdgeInsetsZero。图片内边距,默认边距为0;
-
adjustsImageWhenHighlighted : default is YES. if YES, image is drawn darker when highlighted(pressed),当按钮高亮时是否调整图片。默认是 YES
-
adjustsImageWhenDisabled:default is YES. if YES, image is drawn lighter when disabled.默认是 yes,如果是 YES,图形绘制较轻时禁用
-
showsTouchWhenHighlighted:default is NO. if YES, show a simple feedback (currently a glow) while highlighted。默认是 NO,如果是 YES,在高亮期间显示一个简单的反馈(当前发光).如果没有高亮状态,不做任何反馈。
-
tintColor: The tintColor is inherited through the superview hierarchy. See UIView for more information. 这个属性是从 UIView 继承过来的,更多解释信息见 UIView.该属性不应用于UIButtonTypeCustom.,如果Custom 类型的按钮需要使用该属性,应该重写该属性的 set方法。
-
buttonType:按钮样式属性,取值枚举类型
-
currentTitle:normal/highlighted/selected/disabled. can return nil。获取当前的文本。
-
currentTitleColor:normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)。当前文本颜色,默认白色。
-
currentTitleShadowColor:normal/highlighted/selected/disabled.当前状态下的文本阴影颜色
-
currentImage:normal/highlighted/selected/disabled. can return nil。当前状态下的图片。
-
currentBackgroundImage:normal/highlighted/selected/disabled. can return nil。当前状态下的背景图片
-
currentAttributedTitle:normal/highlighted/selected/disabled. can return nil。当前状态下的文本属性。
-
titleLabel:文本标签属性
-
imageView:图片属性
二、 方法
由于按钮是有状态的,比如:未点击状态,高亮状态,选中状态,不可用状态等。所以按钮所提供的方法当中很多是根据按钮状态来设置的。-
buttonWithType:类方法,创建按钮对象时,直接指定按钮类型。取值为枚举。
-
setTitle:forState:default is nil. title is assumed to be single line。根据按钮状态设置文字。
-
setTitleColor:forState:default if nil. use opaque white。根据按钮状态设置文字颜色,默认白色。
-
setTitleShadowColor: forState: default is nil. use 50% black。 设置文字阴影颜色,默认半透明黑色
-
setImage: forState: :default is nil. should be same size if different for different states。根据按钮状态设置图片
-
setBackgroundImage: forState: 根据按钮状态设置背景图片
-
setAttributedTitle: forState : default is nil. title is assumed to be single line.根据按钮状态设置文本属性内容(包括文字大小,颜色等)。假设文字是单行的
-
titleForState: these getters only take a single state value。根据按钮状态获取文本
-
titleColorForState: 根据按钮状态获取文本颜色
-
titleShadowColorForState: 根据按钮状态获取文本阴影颜色
-
imageForState: 根据按钮状态获取图片
-
backgroundImageForState: 根据按钮状态获取背景图片
-
attributedTitleForState: 根据按钮状态获取文本属性
-
backgroundRectForBounds: :自定义按钮时,可以更改背景图片在按钮当中的位置
-
contentRectForBounds: 自定义按钮时,可以更改整个内容在按钮当中的位置
-
titleRectForContentRect: 自定义按钮时,可以更改Label在按钮当中的位置
-
imageRectForContentRect: 自定义按钮时,可以更改图片在按钮当中的位置