UIButton简介
UIButton 是响应用户交互操作的控件。
UIButton创建方式
除了继承自 UIView 的 init 方法以外,UIButton 还提供了一种创建方式:
+ (instancetype)buttonWithType:(UIButtonType)buttonType;
在创建时需要选择 UIButton 的类型,即 UIButtonType。
UIButtonType为一个枚举类型,其中包含:
- UIButtonTypeCustom : 自定义类型
- UIButtonTypeSystem :矩形背景,蓝色字体
- UIButtonTypeDetailDisclosure :详细说明按钮
- UIButtonTypeInfoLight : 一个亮底的信息按钮
- UIButtonTypeInfoDark : 一个暗底的信息按钮
- UIButtonTypeContactAdd : 添加按钮
- UIButtonTypeRoundedRect :圆角矩形背景
可以根据程序需求,选择相应的
Button类型。
同时也可以对实例化的 UIButton 对象的 buttonType 进行更改,达到同样的效果。
titleLable 相关属性和方法
UIButton 实际上包含了一个 UILable 和一个 UIImageView 分别用来显示文字和图像。我们可以调用方法,修改这些属性的数据。
首先 UIButton 具有不同的状态,如被选中时,禁用时等,这些状态被包括在一个枚举类型 UIControlState 中,其中常用的有:
- UIControlStateNormal : 普通状态
- UIControlStateHighlighted : 高亮状态
- UIControlStateDisabled : 禁用状态
- UIControlStateSelected : 被选择状态
以下是 titleLable 相关属性和方法
- 修改
UIButton在不同状态下的标题
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- 获取
UIButton在不同状态下的标题
- (NSString *)titleForState:(UIControlState)state;
- 使用富文本设置不同状态下
UIButton的标题
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state;
- 获取不同状态下
UIButton的富文本标题
- (NSAttributedString *)attributedTitleForState:(UIControlState)state;
- 设置不同状态下
UIButton的标题颜色
- (UIColor *)titleColorForState:(UIControlState)state;
- 设置不同状态下
UIButton标题的阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
- 获取不同状态下
UIButton标题的阴影颜色
- (UIColor *)titleShadowColorForState:(UIControlState)state;
- 布尔类型,在
UIButton为highlighted情况下是否改变标题的阴影
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;
UIButton 配置方法与属性
- 布尔类型,当
UIButton为highlighted情况下是否发生变化
@property(nonatomic) BOOL adjustsImageWhenHighlighted;
- 布尔类型,当
UIButton为disabled情况下是否发生变化
@property(nonatomic) BOOL adjustsImageWhenDisabled;
- 布尔类型,当
UIButton为highlighted情况下是否外发光
@property(nonatomic) BOOL showsTouchWhenHighlighted;
- 设置不同状态下
UIButton的背景图片
- (UIImage *)backgroundImageForState:(UIControlState)state;
- 获取不同状态下
UIButton的背景图片
- (UIImage *)imageForState:(UIControlState)state;
UIButton 配置边缘的属性
- 配置
UIButton的边缘
@property(nonatomic) UIEdgeInsets contentEdgeInsets;
- 配置
UIButton内titleLabel的边缘
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
- 配置
UIButton内imageView的边缘
@property(nonatomic) UIEdgeInsets imageEdgeInsets;
UIButton 获取当前状态的属性
- 获取当前
UIButton类型
@property(nonatomic, readonly) UIButtonType buttonType;
- 获取当前
UIButton标题
@property(nonatomic, readonly, strong) NSString *currentTitle;
- 获取当前
UIButton富文本标题
@property(nonatomic, readonly, strong) NSAttributedString *currentAttributedTitle;
- 获取当前
UIButton标题颜色
@property(nonatomic, readonly, strong) UIColor *currentTitleColor;
- 获取当前
UIButton标题阴影颜色
@property(nonatomic, readonly, strong) UIColor *currentTitleShadowColor;
- 获取当前
UIButton中imageView显示的图片
@property(nonatomic, readonly, strong) UIImage *currentImage;
- 获取当前
UIButton的背景图片
@property(nonatomic, readonly, strong) UIImage *currentBackgroundImage;
- 获取当前
UIButton中的imageView
@property(nonatomic, readonly, strong) UIImageView *imageView;
UIButton 定义尺寸方法
- 定义
UIButton背景的尺寸
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- 定义
UIButton内容的尺寸
- (CGRect)contentRectForBounds:(CGRect)bounds;
- 定义
UIButton中imageView的frame属性
-(CGRect)imageRectForContentRect:(CGRect)contentRect;
- 定义
UIButton中titleLabel的frame属性
-(CGRect)titleRectForContentRect:(CGRect)contentRect;