美文网首页
iOS开发UI篇-常用控件详解(UIButton)

iOS开发UI篇-常用控件详解(UIButton)

作者: willphonez | 来源:发表于2016-07-20 13:04 被阅读173次

    一. 常用属性


    • IButton内有两个控件titleLabelimageView,可以用来显示一个文本和图片,给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改变水平对齐方式。
      想改变两个子控件的显示位置,可以分别通过setTitleEdgeInsetssetImageEdgeInsets来实现。需要注意的是,对titleLabelimageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离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:按钮样式属性,取值枚举类型

    UIButtonTypeCustom:无类型,按钮的内容需要自定义
    UIButtonTypeDetailDisclosure:
    UIButtonTypeInfoLight:
    UIButtonTypeInfoDark:
    UIButtonTypeContactAdd:

    • 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:图片属性

    三. 常用方法


    由于按钮是有状态的,比如:未点击状态,高亮状态,选中状态,不可用状态等。所以按钮所提供的方法当中很多是根据按钮状态来设置的。

    normal(普通状态)

    默认情况(Default)
    对应的枚举常量:UIControlStateNormal

    highlighted(高亮状态)
    按钮被按下去的时候(手指还未松开)
    对应的枚举常量:UIControlStateHighlighted

    disabled(失效状态,不可用状态)
    如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
    对应的枚举常量:UIControlStateDisabled

    • buttonWithType:类方法,创建按钮对象时,直接指定按钮类型。取值为枚举。

    • setTitle:forState: 根据按钮状态设置文字。

    • setTitleColor:forState: 根据按钮状态设置文字颜色,默认白色。

    • setTitleShadowColor: forState: 设置文字阴影颜色,默认半透明黑色

    • setImage: forState: : 根据按钮状态设置图片

    • setBackgroundImage: forState: 根据按钮状态设置背景图片

    • setAttributedTitle: forState : 根据按钮状态设置文本属性内容(包括文字大小,颜色等)。假设文字是单行的

    • titleForState:根据按钮状态获取文本

    • titleColorForState: 根据按钮状态获取文本颜色

    • titleShadowColorForState: 根据按钮状态获取文本阴影颜色

    • imageForState: 根据按钮状态获取图片

    • backgroundImageForState: 根据按钮状态获取背景图片

    • attributedTitleForState: 根据按钮状态获取文本属性

    • backgroundRectForBounds::自定义按钮时,可以更改背景图片在按钮当中的位置

    • contentRectForBounds:自定义按钮时,可以更改整个内容在按钮当中的位置

    • titleRectForContentRect: 自定义按钮时,可以更改Label在按钮当中的位置

    • imageRectForContentRect: 自定义按钮时,可以更改图片在按钮当中的位置

    相关文章

      网友评论

          本文标题:iOS开发UI篇-常用控件详解(UIButton)

          本文链接:https://www.haomeiwen.com/subject/bbyvjttx.html