#pragma mark ****************常用属性****************
@property(nonatomic,getter=isEnabled) BOOL enabled; // 默认 YES.
@property(nonatomic,getter=isSelected) BOOL selected; // 默认 NO
@property(nonatomic,getter=isHighlighted) BOOL highlighted; // 默认 NO.
// 设置背景色
button.backgroudColor=[UIColor clearColor]; //可以去掉背景色
// Button添加边框
button.layer.borderColor = [[UIColor grayColor] CGColor];
button.layer.borderWidth = 2;
// 设置圆角
button.layer.cornerRadius = 5;
button.layer.masksToBounds = YES;
button.clipsToBounds = YES;
// 设置是否能与用户进行交互
button.userInteractionEnabled = YES;
#pragma mark ****************其他属性****************
//标题的阴影改变时,按钮是否高亮显示。默认为NO
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted
//按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES
@property(nonatomic) BOOL adjustsImageWhenHighlighted
//按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES
@property(nonatomic) BOOL adjustsImageWhenDisabled
//按下按钮是否会发光。默认是NO
@property(nonatomic) BOOL showsTouchWhenHighlighted
//设置按钮的内部标题离按钮边缘上下左右的距离
@property(nonatomic) UIEdgeInsets titleEdgeInsets
//设置按钮的内部图片离按钮边缘上下左右的距离
@property(nonatomic) UIEdgeInsets imageEdgeInsets
//设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。
@property(nonatomic) UIEdgeInsets contentEdgeInsets
#pragma mark ****************获取按钮当前状态下的配置****************
//获取按钮类型,只读属性
@property(nonatomic, readonly) UIButtonType buttonType
//获取按钮当前标题,只读属性
@property(nonatomic, readonly, retain) NSString *currentTitle
//获取按钮当前的富文本标题
@property(nonatomic, readonly, retain) NSAttributedString *currentAttributedTitle
//获取当前标题的颜色
@property(nonatomic, readonly, retain) UIColor *currentTitleColor
//获取当前标题的阴影颜色
@property(nonatomic, readonly, retain) UIColor *currentTitleShadowColor
//获取当前按钮的图片
@property(nonatomic, readonly, retain) UIImage *currentImage
//获取当前按钮的背景图片
@property(nonatomic, readonly, retain) UIImage *currentBackgroundImage
//获取当前按钮的图片框对象
@property(nonatomic, readonly, retain) UIImageView *imageView
#pragma mark ****************设置方法****************
//设置按钮在某个状态下的标题文字
- (void)setTitle:(NSString *)title forState:(UIControlState)state
//设值按钮在某个状态下的富文本标题
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state
//设置按钮标题的颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
//设置某个状态下按钮标题的阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
//设置按钮的背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
#pragma mark ****************获取按钮配置方法****************
//返回按钮在某个状态下的标题颜色
- (UIColor *)titleColorForState:(UIControlState)state
//获取按钮的填充图片
- (UIImage *)imageForState:(UIControlState)state
//返回按钮在某个状态下的背景图片
- (UIImage *)backgroundImageForState:(UIControlState)state
#pragma mark ****************常用属性的枚举值****************
// UIButton事件
typedef enum {
UIControlEventTouchDown // 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat // 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside // 当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside // 当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter // 当一次触摸从控件窗口之外拖动到内部时
UIControlEventTouchDragExit // 当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside // 所有在控件之内触摸抬起事件
UIControlEventTouchUpOutside // 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel // 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventValueChanged // 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知
UIControlEventEditingDidBegin // 当文本控件中开始编辑时发送通知
UIControlEventEditingChanged // 当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd // 当文本控件中编辑结束时发送通知。
UIControlEventEditingDidEndOnExit // 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAllTouchEvents // 通知所有触摸事件。
UIControlEventAllEditingEvents // 通知所有关于文本编辑的事件。
UIControlEventApplicationReserved // range available for application use
UIControlEventSystemReserved // range reserved for internal framework use
UIControlEventAllEvents // 通知所有事件
}
// 状态: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现以下是几种状态
typedef enum {
UIControlStateNormal = 0, //常规状态显现
UIControlStateHighlighted = 1 << 0, //高亮状态显现
UIControlStateDisabled = 1 << 1, //禁用的状态才会显现
UIControlStateSelected = 1 << 2, //选中状态
UIControlStateApplication = 0x00FF0000, //当应用程序标志时
UIControlStateReserved = 0xFF000000 //为内部框架预留,可以不管他
};
// 能够定义的button类型有以下6种,
typedef enum {
UIButtonTypeCustom = 0, //自定义风格
UIButtonTypeDetailDisclosure, //蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight, //亮色感叹号
UIButtonTypeInfoDark, //暗色感叹号 白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd, //蓝色加号+按钮
UIButtonTypeSystem //系统默认样式,若使用此样式,在使用setImage时会显示异常
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead //圆角矩形
} UIButtonType;
#pragma mark ****************重写绘制行为****************
//你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。
//注意:不要直接调用这些方法, 这些方法是你写给系统调用的。
//指定背景边界
- (CGRect)backgroundRectForBounds:(CGRect)bounds
//指定内容边界
- (CGRect)contentRectForBounds:(CGRect)bounds
//指定文字标题边界
- (CGRect)titleRectForContentRect:(CGRect)contentRect
//指定按钮图像边界
- (CGRect)imageRectForContentRect:(CGRect)contentRect
//示例:
- (CGRect)imageRectForContentRect:(CGRect)bounds{
return CGRectMake(0.0, 0.0, 44, 44);
}
UIButton 整理
最新推荐文章于 2020-12-19 08:38:26 发布