天天看點

iOS UIButton解讀

UIButton控件是應用界面中常用的一個控件,用法總結:

一、初始化

UIButton的初始化一般使用其類方法,+ (id)buttonWithType:(UIButtonType)buttonType;

風格的枚舉如下:

typedef NS_ENUM(NSInteger, UIButtonType) {

//使用者自定義,無風格
UIButtonTypeCustom = 0,  
//系統預設風格                       
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), 
//一下這三種建立出來的按鈕一樣,一個藍色的圓圈,中間有個歎号
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
//建立+号按鈕
UIButtonTypeContactAdd,
//廢棄
UIButtonTypeRoundedRect = UIButtonTypeSystem,           

};

二、屬性設定

@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;

//這個屬性設定button裡内容的偏移量,包括title和image,可以用如下方法設定

btn.contentEdgeInsets=UIEdgeInsetsMake(20, 20, 0, 0);

@property(nonatomic) UIEdgeInsets titleEdgeInsets;

//這個屬性設定标題的偏移量

@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;

//按鈕高亮時,是否改變陰影效果

@property(nonatomic) UIEdgeInsets imageEdgeInsets;

//圖檔的偏移量

@property(nonatomic)BOOL adjustsImageWhenHighlighted;

//設定圖檔的繪制是否高亮時變暗

@property(nonatomic)BOOL adjustsImageWhenDisabled;

//設定圖檔是否輕繪制當按鈕禁用時

@property(nonatomic)BOOL showsTouchWhenHighlighted;

//設定是否顯示手指印在按鈕高亮的時候

@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0);

//這個屬性會作用于标題和圖檔,但是如果你是自定義風格的按鈕,這個屬性将不起任何作用,它隻作用于系統的

@property(nonatomic,readonly) UIButtonType buttonType;

//設定button的風格

三、一些set方法

  • (void)setTitle:(NSString *)title forState:(UIControlState)state;

//設定标題和顯示目前标題的按鈕狀态

  • (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

//設定标題顔色和顯示目前顔色的按鈕狀态

  • (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;

//設定标題陰影顔色及顯示時的狀态

  • (void)setImage:(UIImage *)image forState:(UIControlState)state;

//設定按鈕圖檔和顯示目前圖檔時的狀态

  • (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

//設定按鈕背景圖檔和顯示圖檔時的狀态

  • (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

//通過AttributeString建立标題

注意:按鈕圖檔設定和背景圖檔的不同在于:

1、設定圖檔,如果有标題會和标題并列顯示

    2、設定背景圖檔會出現在标題下面

    3、圖檔的偏移量可以設定,背景圖檔不可以。
           

四、一些get方法,可以得到上述設定的屬性

  • (NSString *)titleForState:(UIControlState)state;
  • (UIColor *)titleColorForState:(UIControlState)state;
  • (UIColor *)titleShadowColorForState:(UIControlState)state;
  • (UIImage *)imageForState:(UIControlState)state;
  • (UIImage *)backgroundImageForState:(UIControlState)state;
  • (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

五、一些隻讀屬性

@property(nonatomic,readonly,retain) NSString *currentTitle;

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

@property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor;

@property(nonatomic,readonly,retain) UIImage *currentImage;

@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage;

@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);

//這兩個參數需要注意,雖然他們是隻讀屬性不能重新設定,但是我們可以設定label和imageView的相關屬性

@property(nonatomic,readonly,retain) UILabel *titleLabel NS_AVAILABLE_IOS(3_0);

@property(nonatomic,readonly,retain) UIImageView *imageView NS_AVAILABLE_IOS(3_0);

六、下面這些函數,都會傳回一個CGRect 矩形範圍

  • (CGRect)backgroundRectForBounds:(CGRect)bounds;

//傳回背景大小

  • (CGRect)contentRectForBounds:(CGRect)bounds;

//傳回視圖大小,包括标題和圖檔

  • (CGRect)titleRectForContentRect:(CGRect)contentRect;

//傳回标題大小

  • (CGRect)imageRectForContentRect:(CGRect)contentRect;

//傳回圖檔大小

關于觸發事件,button是繼承于UIControl,這裡不再叙述。

繼續閱讀