一、概述
1. 說明
UIButton 繼承 UIControl(基本控件類)
2. 屬性
+ (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 //內部框架預留,無意義
};
- (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;