IOS UIButton解読

3742 ワード

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に継承されているが,ここでは述べない.