【学习笔记】UIButton

来源:互联网 发布:雷霆vs勇士第六场数据 编辑:程序博客网 时间:2024/06/07 22:55

//  UIButton.h

 

#import <Foundation/Foundation.h>

#import <UIKit/UIControl.h>

#import <UIKit/UIGeometry.h>

#import <UIKit/UIStringDrawing.h>

#import <UIKit/UIKitDefines.h>

 

@class UIImage, UIFont,UIColor,UIImageView,UILabel;

 

//button样式选择,默认是无

typedef NS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom = 0,               自定义风格(无边框的那种)

    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), 圆角矩形(系统的那种)

    UIButtonTypeDetailDisclosure,                ios7上变成一个亮的蓝色感叹号)

    UIButtonTypeInfoLight,                      亮色感叹号

    UIButtonTypeInfoDark,                      暗色感叹号

    UIButtonTypeContactAdd,                        十字加号按钮

};


NS_CLASS_AVAILABLE_IOS(2_0)@interfaceUIButton : UIControl <NSCoding>

+ (id)buttonWithType:(UIButtonType)buttonType;


 

//创建button,选择样式

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

 

//位置

button1.frame = CGRectMake(100,100,50,50);  


[self.view  add   :buttob ];

 


内容到边缘距离(上左下右)

@property(nonatomic)         UIEdgeInsets contentEdgeInsetsUI_APPEARANCE_SELECTOR;

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

标题到边缘距离,同上

@property(nonatomic)         UIEdgeInsets titleEdgeInsets;               

图片到边缘距离,同上

@property(nonatomic)         UIEdgeInsets imageEdgeInsets;               

 


确定按钮高亮时是否改变阴影的Bool值.默认时NO

@property(nonatomic)         BOOL         reversesTitleShadowWhenHighlighted;

 

图片随按钮高亮而高亮,默认YES

@property(nonatomic)         BOOL         adjustsImageWhenHighlighted;  

btton.adjustsImageWhenHighlighted = NO;

图片随按钮失效而变暗,默认YES

@property(nonatomic)         BOOL         adjustsImageWhenDisabled;     

按下会有白色光点,默认NO

@property(nonatomic)         BOOL         showsTouchWhenHighlighted;  

 

 

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

// The tintColor is inherited through the superview hierarchy.See UIView for more information.

 

 

//标题文字

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

[button1 setTitle:@"BTN1" forState:UIControlStateNormal];  

 

// 标题颜色,默认正常黑

- (void)setTitleColor:(UIColor*)color forState:(UIControlState)stateUI_APPEARANCE_SELECTOR;

[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  

 

 // 阴影颜色,默认50%

- (void)setTitleShadowColor:(UIColor*)color forState:(UIControlState)stateUI_APPEARANCE_SELECTOR;

[button1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];  

 

// 设置图片,原来大小,默认无

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

//[button1 setImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal];  

 

// 设置背景图片,拉伸或压缩,默认无

- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)stateUI_APPEARANCE_SELECTOR;

[button1 setBackgroundImage:[UIImage imageNamed:@"P"] forState:UIControlStateHighlighted];  

 

//上述5个对应选择状态

- (void)setAttributedTitle:(NSAttributedString*)title forState:(UIControlState)stateNS_AVAILABLE_IOS(6_0);

- (NSString *)titleForState:(UIControlState)state;        

- (UIColor *)titleColorForState:(UIControlState)state;

- (UIColor *)titleShadowColorForState:(UIControlState)state;

- (UIImage *)imageForState:(UIControlState)state;

- (UIImage *)backgroundImageForState:(UIControlState)state;

 

 

返回在某个状态下,按钮标题的富文本

- (NSAttributedString *)attributedTitleForState:(UIControlState)stateNS_AVAILABLE_IOS(6_0);

NSAttributedString *attributedString = [button attributedTitleForState:UIControlStateNormal];

 

 

得到当前状态(只读)

@property(nonatomic,readonly)UIButtonType buttonType;

 

//当前按钮上显示的标题(只读),当按钮状态改变时值自动改变.值可以为nil.

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

 

当前标题颜色(只读).此值要保证不为nil,默认是白色

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

 

 // 标题的阴影颜色(只读).默认是白色

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

 

当前按钮上的图片(只读).可以是nil.

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

 

// 当前按钮背景图片(只读).可以是nil.

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

 

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

 

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

 

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

 

 

 

 

 

 

 

得到尺寸

 

返回背景绘制区域

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

 

返回内容绘制区域.内容区域是显示图片和标题及他们特定对齐缩放等的范围

- (CGRect)contentRectForBounds:(CGRect)bounds;

 

返回标题的绘制区域.

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

 

返回图片的绘制区域.

- (CGRect)imageRectForContentRect:(CGRect)contentRect;

 

 

 

@end

 

 

 

 

 

@interfaceUIButton(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          titleShadowOffsetNS_DEPRECATED_IOS(2_0,3_0);

 

@end

 

 

 

 

UIControlState  状态

1       UIControlStateNormal       = 0,  

2      UIControlStateHighlighted  = 1 << 0,        // 高亮  

3       UIControlStateDisabled     = 1 << 1,  

4      UIControlStateSelected     = 1 << 2,                  // 选中  

5      UIControlStateApplication  = 0x00FF0000,              // 当应用程序标志使用时  

6       UIControlStateReserved     = 0xFF000000               // 为内部框架预留 

 

 

 

 

UIControlEvents  事件方法

  1.  UIControlEventTouchDown           = 1 <<  0,      
  2.     UIControlEventTouchDownRepeat     = 1 <<  1,     
  3.     UIControlEventTouchDragInside     = 1 <<  2,  
  4.     UIControlEventTouchDragOutside    = 1 <<  3,  
  5.     UIControlEventTouchDragEnter      = 1 <<  4,  
  6.     UIControlEventTouchDragExit       = 1 <<  5,  
  7.     UIControlEventTouchUpInside       = 1 <<  6,          基本就用这个
  8.     UIControlEventTouchUpOutside      = 1 <<  7,  
  9.     UIControlEventTouchCancel         = 1 <<  8,  
  10.   
  11.     UIControlEventValueChanged        = 1 << 12,        
  12.     UIControlEventEditingDidBegin     = 1 << 16,      
  13.     UIControlEventEditingChanged      = 1 << 17,  
  14.     UIControlEventEditingDidEnd       = 1 << 18,  
  15.     UIControlEventEditingDidEndOnExit = 1 << 19,       
  16.     UIControlEventAllTouchEvents      = 0x00000FFF,    
  17.     UIControlEventAllEditingEvents    = 0x000F0000,    
  18.     UIControlEventApplicationReserved = 0x0F000000, 
  19.     UIControlEventSystemReserved      = 0xF0000000, 
  20.     UIControlEventAllEvents           = 0xFFFFFFFF  

 

0 0
原创粉丝点击