iOS——UIButton

来源:互联网 发布:java 静态导入 编辑:程序博客网 时间:2024/06/04 20:51

一、概述

1. 说明 

UIButton 继承 UIControl(基本控件类)


2. 属性

1)创建 UIButton 对象并使用指定的风格

+ (instancetype)buttonWithType:(UIButtonType)buttonType;

UIButtonType 枚举如下 :

typedef NS_ENUM(NSInteger, UIButtonType) {        UIButtonTypeCustom = 0,                         // 无风格,创建带图片的按钮需要这种风格        UIButtonTypeSystem,                             // 系统风格        UIButtonTypeDetailDisclosure,                   // 显示详细按钮        UIButtonTypeInfoLight,                          // 亮色感叹号按钮        UIButtonTypeInfoDark,                           // 暗色感叹号按钮        UIButtonTypeContactAdd,                         // 加号按钮        UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 圆角按钮,被废弃,使用 UIButtonTypeSystem 代替    };


2)保存 UIButton 对象的风格

@property(nonatomic,readonly)UIButtonType buttonType;


3)获取按钮的标题label(在按钮上有一个label)

@property(nullable,nonatomic,readonly,strong)UILabel * titleLabel;


4)获取按钮的图片视图

@property(nullable,nonatomic,readonly,strong)UIImageView *imageView;


5)设置按钮的 标题label 的标题和状态

- (void)setTitle:(nullableNSString *)title forState:(UIControlState)state; 

UIControlState 枚举如下 :

typedef NS_OPTIONS(NSUInteger, UIControlState) {        UIControlStateNormal       = 0,          // 正常状态        UIControlStateHighlighted  = 1 << 0,     // 按下状态(当按下按钮时)        UIControlStateDisabled     = 1 << 1,     // 禁用状态(无法响应事件,可以通过 enable 属性设置为 NO 也是此状态)        UIControlStateSelected     = 1 << 2,     // 选中状态(可以响应事件,可以通过 selected 属性设置为 YES 也是此状态)        UIControlStateFocused      = 1 << 3,     // 聚焦状态,iOS9才有        UIControlStateApplication  = 0x00FF0000, // 当用作应用标志时        UIControlStateReserved     = 0xFF000000  //内部框架预留,无意义    };


6)设置按钮的 标题label 的颜色和状态

- (void)setTitleColor:(nullableUIColor *)color forState:(UIControlState)state;


7)设置按钮的 标题label 的引用颜色和状态

- (void)setTitleShadowColor:(nullableUIColor *)color forState:(UIControlState)state ;


8)设置按钮的 图片和状态

- (void)setImage:(nullableUIImage *)image forState:(UIControlState)state; 


9)设置背景图片和状态

- (void)setBackgroundImage:(nullableUIImage *)image forState:(UIControlState)state;


10)设置属性标题和状态

- (void)setAttributedTitle:(nullableNSAttributedString *)title forState:(UIControlState)state;


11)返回指定状态的标题

- (nullableNSString *)titleForState:(UIControlState)state;


12)返回指定状态的标题颜色

- (nullableUIColor *)titleColorForState:(UIControlState)state;


13)返回指定状态的标题阴影颜色

- (nullableUIColor *)titleShadowColorForState:(UIControlState)state;


14)返回指定状态的图片

- (nullableUIImage *)imageForState:(UIControlState)state;


15)返回指定状态的背景图片

- (nullableUIImage *)backgroundImageForState:(UIControlState)state;


16)返回指定状态的属性字符串

- (nullableNSAttributedString *)attributedTitleForState:(UIControlState)state;


17)返回当前状态的标题

@property(nullable,nonatomic,readonly,strong)NSString *currentTitle;


17)返回当前状态的标题颜色

@property(nonatomic,readonly,strong)UIColor  *currentTitleColor;


19)返回当前状态的标题阴影颜色

@property(nullable,nonatomic,readonly,strong)UIColor  *currentTitleShadowColor;


20)返回当前状态的图片

@property(nullable,nonatomic,readonly,strong)UIImage  *currentImage;


21)返回当前状态的背景图片

@property(nullable,nonatomic,readonly,strong)UIImage  *currentBackgroundImage;


22)返回当前状态的属性字符串

@property(nullable,nonatomic,readonly,strong)NSAttributedString *currentAttributedTitle;










0 0