UIKit系列-UIButton 详解

一个UIButton类会截断touchEvents,然后发送一个action给target。设置target和selector的方法继承于UIControl。主要用于文字、图片的点击。 
下面,根据Xcode7 的button,一个个属性来熟悉UIButton。

Type:button的类型。

typedef NS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom = 0,                         // 自定义

    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系统按钮

   //下面这个几个不常用

    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead

};

 

State Config:配置各个状态下的button。

 

    UIControlStateNormal       = 0,          //默认

    UIControlStateHighlighted  = 1 << 0,                  // 高亮,当点击的时候处于高亮

    UIControlStateDisabled     = 1 << 1,       //禁用,enable为NO的时候

    UIControlStateSelected     = 1 << 2,                  // 选中状态(这个必须手动设置)

 

Title:标题。有Plain和AttibuteString两种模式,常用Plain。AttributeString 是属性字符串,详情可以参考Label 的AttributeString。

Font:字体。常用System的字体。

TextColor:文字颜色。

Shadow Color:投影颜色。

Image:button上的图片。

BackGround:背景图片。

Shadow Offset:投影偏移的坐标。

Drawing:四个属性。分别是:高亮时反转阴影(文字会动),高亮时显示点击(会有闪瞎眼的亮光),高亮调整图片(记住,是图片),enable 为false时 调暗图片。

Line Break:标题文字截断模式。(Clip是直接剪切掉后面,还有分行(根据单词、根据字母),截断前面(...trail)、截断中间(head...trail)、截断后面(head middle ...)。(假设标题是 head middle trail)。

Edge:内边框。分别是标题和图片。Inset:上下左右的偏移量。

 

 

 
 
UIButton经常会遇到的两个问题:
1,设置title的时候,文字会闪烁。这个可以把Button的Type改成custom。
2,圆角按钮。可在button的IdentitySepector窗口的UserDefinedRuntimeAttributes  添加一项: layer.cornerRadius   Number  5。
 
 
原文地址:https://www.cnblogs.com/loying/p/5082801.html