概述
UIButton
的父類是UIControl
,UIControl
的父類是UIView
,UIView
的父類是UIResponder
。
API
-
button
的便利構造方法
參數(shù)buttonType
是button
的類型
+ (id)buttonWithType:(UIButtonType)buttonType;
能夠定義的button
類型
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, //自定義風格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //系統(tǒng)樣式,從iOS7開始使用
UIButtonTypeDetailDisclosure, //藍色小箭頭按鈕,主要做詳細說明用
UIButtonTypeInfoLight, //亮色感嘆號
UIButtonTypeInfoDark, //暗色感嘆號
UIButtonTypeContactAdd, //十字加號按鈕
UIButtonTypeRoundedRect = UIButtonTypeSystem, //圓角矩形,從iOS7廢棄,iOS6中可以使用
}; -
button
的內容偏移量。默認為UIEdgeInsetsZero
(我習慣稱它為偏移量)
UIEdgeInsets
就是插入間隔區(qū)域。正值表示間隔值,負值表示超出參照物的距離。
UIEdgeInsetsMake(top, left, bottom, right)
有四個值需要設置,分別距離上左下右邊的間隔。
@property(nonatomic) UIEdgeInsets contentEdgeInsets; -
button
的標題偏移量。這個屬性和圖片偏移量是相對的,比如:自定義一個按鈕實現(xiàn)的效果是圖片在左邊,標題在右邊,可以用這個屬性,設置完標題偏移量,圖片偏移量就是相對于標題的。默認為UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets titleEdgeInsets; -
button
的狀態(tài)為高亮時,文本的陰影會反轉。默認是NO
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; -
button
的圖片偏移量。默認為UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets imageEdgeInsets; -
button
的狀態(tài)為高亮時,圖像變暗。默認是YES
@property(nonatomic) BOOL adjustsImageWhenHighlighted; -
button
的狀態(tài)為禁用時,圖像變暗。默認是YES
@property(nonatomic) BOOL adjustsImageWhenDisabled; -
button
的狀態(tài)為高亮時,發(fā)光。默認是NO
@property(nonatomic) BOOL showsTouchWhenHighlighted; - iOS6中高亮顏色,默認是藍色。iOS7中系統(tǒng)的一些樣式
DetailDisclosure
InfoLight
InfoDark
ContactAdd
顏色會改變,其他的沒看到效果。
@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0); -
button
的樣式
@property(nonatomic,readonly) UIButtonType buttonType; - 設置
button
的標題,后面參數(shù)是狀態(tài),在不同的狀態(tài)顯示不同的標題。
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
button
的狀態(tài)。包括一些其他的控制的狀態(tài)
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0, //正常狀態(tài)
UIControlStateHighlighted = 1 << 0, //高亮狀態(tài)
UIControlStateDisabled = 1 << 1, //禁用狀態(tài)
UIControlStateSelected = 1 << 2, //選中狀態(tài)
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
}; - 設置
button
的標題顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; - 設置
button
的標題陰影顏色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; - 設置
button
的圖片。比如設置左邊圖片右邊標題,則使用這個方法,而不是設置背景圖。
- (void)setImage:(UIImage *)image forState:(UIControlState)state; - 設置
button
的背景圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; - 設置
button
的多樣化標題。比如中間一個字字體為10
號顏色為紅色,其他字為綠色。
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); - 返回
button
的標題。在不同狀態(tài)的標題
- (NSString *)titleForState:(UIControlState)state; - 返回
button
的標題顏色
- (UIColor *)titleColorForState:(UIControlState)state; - 返回
button
的標題陰影顏色
- (UIColor *)titleShadowColorForState:(UIControlState)state; - 返回
button
的圖片
- (UIImage *)imageForState:(UIControlState)state; - 返回
button
的背景圖片
- (UIImage *)backgroundImageForState:(UIControlState)state; - 返回
button
的多樣化標題
- (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0); -
button
的當前標題。當按鈕狀態(tài)改變時值自動改變,可以做判斷,當前標題是全文則點擊展開標題設置為收起,當前標題是收起則點擊收起全文。
@property(nonatomic,readonly,retain) NSString *currentTitle; -
button
的當前標題顏色
@property(nonatomic,readonly,retain) UIColor *currentTitleColor; -
button
的當前標題陰影顏色
@property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor; -
button
的當前圖片。切換不同圖片,比如做單選,多選可以使用。
@property(nonatomic,readonly,retain) UIImage *currentImage; -
button
的當前背景圖片
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage; -
button
的當前多樣化標題
@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); -
button
的標題label
。設置字體大小
@property(nonatomic,readonly,retain) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); -
button
的圖片視圖
@property(nonatomic,readonly,retain) UIImageView *imageView NS_AVAILABLE_IOS(3_0); - 返回背景繪制區(qū)域
- (CGRect)backgroundRectForBounds:(CGRect)bounds; - 返回內容繪制區(qū)域。內容區(qū)域是顯示圖片和標題及他們特定對齊縮放等的范圍
- (CGRect)contentRectForBounds:(CGRect)bounds; - 返回標題的繪制區(qū)域
- (CGRect)titleRectForContentRect:(CGRect)contentRect; - 返回圖片的繪制區(qū)域
- (CGRect)imageRectForContentRect:(CGRect)contentRect; - 下面的屬性都已廢棄
@interface UIButton(UIButtonDeprecated)
@property(nonatomic,retain) UIFont *font NS_DEPRECATED_IOS(2_0, 3_0);
@property(nonatomic) NSLineBreakMode lineBreakMode NS_DEPRECATED_IOS(2_0, 3_0);
@property(nonatomic) CGSize titleShadowOffset NS_DEPRECATED_IOS(2_0, 3_0);