iOS-UIKit框架學習—UIButton

weixin_34124651發表於2017-03-08

UIButton的類的一個例項,實現了在觸控式螢幕上的按鈕。觸控一個按鈕攔截事件和動作訊息傳送到目標物件時。設定的目
和行動方法都繼承自UIControl。這個類提供了方法來設定標題,影像,按鈕等外觀屬性。通過這些訪問,您可以為每個按鈕狀態指定一個不同的外觀。

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl <NSCoding>

// 設定按鈕型別
+ (instancetype)buttonWithType:(UIButtonType)buttonType;
// 按鈕內容與邊緣的距離
@property(nonatomic)          UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;
// 設定標題與邊緣或插圖的距離
@property(nonatomic)          UIEdgeInsets titleEdgeInsets;
// 當按鈕Highlighted狀態時是否標題陰影更加突出
@property(nonatomic)          BOOL         reversesTitleShadowWhenHighlighted;
// 圖片與邊緣的距離
@property(nonatomic)          UIEdgeInsets imageEdgeInsets;
// 當按鈕Highlighted狀態時是否圖片突出 預設值YES
@property(nonatomic)          BOOL         adjustsImageWhenHighlighted;
// 當按鈕Disabled狀態時是否改變圖片
@property(nonatomic)          BOOL         adjustsImageWhenDisabled;
// 點選按鈕是否有高亮效果 預設值NO
@property(nonatomic)          BOOL         showsTouchWhenHighlighted __TVOS_PROHIBITED;
// 要應用到的按鈕標題和影像的色調顏色
@property(null_resettable, nonatomic,strong)   UIColor     *tintColor NS_AVAILABLE_IOS(5_0);
// 按鈕型別
@property(nonatomic,readonly) UIButtonType buttonType;
// 設定指定狀態下的按鈕標題
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
// 設定指定狀態下按鈕標題的顏色
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
// 設定指定狀態下按鈕陰影的顏色
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
// 設定指定狀態下按鈕的圖片
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
// 設定指定狀態下按鈕的背景圖片
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
// 設定指定狀態下按鈕的屬性標題
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
// 獲取指定狀態下按鈕標題
- (nullable NSString *)titleForState:(UIControlState)state;
// 獲取指定狀態下按鈕標題顏色
- (nullable UIColor *)titleColorForState:(UIControlState)state;
// 獲取指定狀態下按鈕陰影顏色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
// 獲取指定狀態下按鈕圖片
- (nullable UIImage *)imageForState:(UIControlState)state;
// 獲取指定狀態下按鈕背景圖片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;
// 獲取指定狀態下按鈕標題屬性
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
// 獲取當前按鈕的標題
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;
// 獲取當前按鈕標題顏色
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;
// 獲取當前按鈕陰影邊框的顏色
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor;
// 獲取當前按鈕的圖片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;
// 獲取當前按鈕背景圖片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;
// 獲取當前按鈕標題屬性
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);

// 獲取當前按鈕標題Label
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
// 獲取當前按鈕的imageView
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);
// 返回按鈕的背景的CGRect
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
// 返回按鈕內容的CGRect
- (CGRect)contentRectForBounds:(CGRect)bounds;
// 返回按鈕標題內容的CGRect
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
// 返回按鈕image的CGRect
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
@end
// 按鈕風格型別
typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         // 自定義型別無樣式
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系統型別
    UIButtonTypeDetailDisclosure, // 藍色小箭頭按鈕,主要做詳細說明用
    UIButtonTypeInfoLight,  // 亮色感嘆號
    UIButtonTypeInfoDark,   // 暗色感嘆號
    UIButtonTypeContactAdd, //  十字加號按鈕
    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 圓角矩形
};

相關文章