字?jǐn)?shù)1443 閱讀548 評(píng)論5 喜歡36
UIButton 的全面解析
建議收藏橄浓,用到的時(shí)候來(lái)這里一查就都明白了
初始化Button 不用alloca init 的方法 用便利構(gòu)造器初始化
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, -自定義風(fēng)格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
UIButtonTypeDetailDisclosure, -藍(lán)色小箭頭按鈕蜜笤,主要做詳細(xì)說(shuō)明
UIButtonTypeInfoLight, -亮色感嘆號(hào)
UIButtonTypeInfoDark, -暗色感嘆號(hào)
UIButtonTypeContactAdd, -十字加號(hào)按鈕
UIButtonTypeRoundedRect = UIButtonTypeSystem, -圓角矩形
};
//設(shè)置button frmae
button.frame = CGRectMake(100, 100, 100, 100);
//設(shè)置button 背景顏色
button.backgroundColor = [UIColor orangeColor];
//添加button的標(biāo)題
[button setTitle:@"登陸" forState:UIControlStateNormal];
//設(shè)置標(biāo)題的顏色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設(shè)置標(biāo)題字體的大小
button.titleLabel.font = [UIFont systemFontOfSize:20];
//設(shè)置button的背景圖片
[button setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
//獲取指定狀態(tài)下的背景圖片
UIImage *tempImage = [button imageForState:UIControlStateNormal];
//設(shè)置前景圖片 前景圖片必須是鏤空?qǐng)D蝗羊,或者是線條勾勒的圖片
[button setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
//設(shè)置陰影顏色
[button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
//button 一些其他屬性
@property(nonatomic) UIEdgeInsets contentEdgeInsets; 內(nèi)容內(nèi)距離
@property(nonatomic) UIEdgeInsets titleEdgeInsets; 標(biāo)題內(nèi)距離
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; 標(biāo)題的陰影改變時(shí)铐姚,按鈕是否高亮顯示哥倔。默認(rèn)為NO
@property(nonatomic) UIEdgeInsets imageEdgeInsets; 圖片內(nèi)邊距
@property(nonatomic) BOOL adjustsImageWhenHighlighted;按鈕高亮的情況下兜喻,圖像的顏色是否要加深一點(diǎn)涩哟。默認(rèn)是YES
@property(nonatomic) BOOL adjustsImageWhenDisabled; 按鈕禁用的情況下,圖像的顏色是否要加深一點(diǎn)侵贵。默認(rèn)是YES
@property(nonatomic) BOOL showsTouchWhenHighlighted; 按下按鈕是否會(huì)發(fā)光 默認(rèn)是NO
@property(nonatomic,readonly) UIButtonType buttonType; button的類(lèi)型
設(shè)置button某個(gè)狀態(tài)的標(biāo)題
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
例: [button setTitle:@"開(kāi)燈" forState:UIControlStateNormal];
設(shè)置button某個(gè)狀態(tài)的標(biāo)題顏色
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state
例:[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
設(shè)置button某個(gè)狀態(tài)陰影的標(biāo)題顏色
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state
例:[button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
設(shè)置button某個(gè)狀態(tài)圖片
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
設(shè)置button 某個(gè)狀態(tài)背景圖片
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil
設(shè)置button 某個(gè)狀態(tài)下的富文本標(biāo)題
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line
返回button 某個(gè)狀態(tài)下的標(biāo)題
- (nullable NSString *)titleForState:(UIControlState)state;
返回button 某個(gè)狀態(tài)下的標(biāo)題顏色
- (nullable UIColor *)titleColorForState:(UIControlState)state;
返回button 某個(gè)狀態(tài)下的陰影標(biāo)題顏色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
返回button 某個(gè)狀態(tài)下的圖片
- (nullable UIImage *)imageForState:(UIControlState)state;
返回button 某個(gè)狀態(tài)下的背景圖片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;
返回button 某個(gè)狀態(tài)下的富文本標(biāo)題
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
/* forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)*/
以下是幾種狀態(tài)
enum {
UIControlStateNormal = 0, 常規(guī)狀態(tài)顯現(xiàn)
UIControlStateHighlighted = 1 << 0, 高亮狀態(tài)顯現(xiàn)
UIControlStateDisabled = 1 << 1, 禁用的狀態(tài)才會(huì)顯現(xiàn)
UIControlStateSelected = 1 << 2, 選中狀態(tài)
UIControlStateApplication = 0x00FF0000, 當(dāng)應(yīng)用程序標(biāo)志時(shí)
UIControlStateReserved = 0xFF000000 為內(nèi)部框架預(yù)留届搁,可以不管他
};
獲取按鈕當(dāng)前標(biāo)題
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;
獲取按鈕當(dāng)前標(biāo)題顏色
@property(nonatomic,readonly,strong) UIColor *currentTitleColor;
獲取按鈕當(dāng)前陰影標(biāo)題顏色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;
獲取按鈕當(dāng)前按鈕內(nèi)圖像
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage;
獲取按鈕當(dāng)前標(biāo)題背景圖片
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage;
獲取按鈕當(dāng)前標(biāo)題富文本
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0);
指定背景邊界
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
指定內(nèi)容邊界 - (CGRect)contentRectForBounds:(CGRect)bounds;
指定標(biāo)題邊界 - (CGRect)titleRectForContentRect:(CGRect)contentRect;
指定圖片邊界 - (CGRect)imageRectForContentRect:(CGRect)contentRect;
示例:
- (CGRect)imageRectForContentRect:(CGRect)bounds{
return CGRectMake(0.0, 0.0, 44, 44);
}
@end
//給按鈕添加點(diǎn)擊事件
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
枚舉值:
UIControlEventTouchDown // 單點(diǎn)觸摸按下事件:用戶點(diǎn)觸屏幕,或者又有新手指落下的時(shí)候窍育。
UIControlEventTouchDownRepeat // 多點(diǎn)觸摸按下事件卡睦,點(diǎn)觸計(jì)數(shù)大于1:用戶按下第二、三漱抓、或第四根手指的時(shí)候表锻。
UIControlEventTouchDragInside // 當(dāng)一次觸摸在控件窗口內(nèi)拖動(dòng)時(shí)。
UIControlEventTouchDragOutside // 當(dāng)一次觸摸在控件窗口之外拖動(dòng)時(shí)乞娄。
UIControlEventTouchDragEnter // 當(dāng)一次觸摸從控件窗口之外拖動(dòng)到內(nèi)部時(shí)
UIControlEventTouchDragExit // 當(dāng)一次觸摸從控件窗口內(nèi)部拖動(dòng)到外部時(shí)瞬逊。
UIControlEventTouchUpInside // 所有在控件之內(nèi)觸摸抬起事件
UIControlEventTouchUpOutside // 所有在控件之外觸摸抬起事件(點(diǎn)觸必須開(kāi)始與控件內(nèi)部才會(huì)發(fā)送通知)。
UIControlEventTouchCancel // 所有觸摸取消事件仪或,即一次觸摸因?yàn)榉派狭颂嗍种付蝗∠纺鳎蛘弑簧湘i或者電話呼叫打斷。
UIControlEventValueChanged // 當(dāng)控件的值發(fā)生改變時(shí)范删,發(fā)送通知蕾域。用于滑塊、分段控件瓶逃、以及其他取值的控件束铭。你可以配置滑塊控件何時(shí)發(fā)送通知,在滑塊被放下時(shí)發(fā)送厢绝,或者在被拖動(dòng)時(shí)發(fā)送。
UIControlEventEditingDidBegin // 當(dāng)文本控件中開(kāi)始編輯時(shí)發(fā)送通知
UIControlEventEditingChanged // 當(dāng)文本控件中的文本被改變時(shí)發(fā)送通知带猴。
UIControlEventEditingDidEnd // 當(dāng)文本控件中編輯結(jié)束時(shí)發(fā)送通知昔汉。
UIControlEventEditingDidEndOnExit // 當(dāng)文本控件內(nèi)通過(guò)按下回車(chē)鍵(或等價(jià)行為)結(jié)束編輯時(shí),發(fā)送通知。
UIControlEventAllTouchEvents // 通知所有觸摸事件靶病。
UIControlEventAllEditingEvents // 通知所有關(guān)于文本編輯的事件会通。
UIControlEventApplicationReserved // range available for application use
UIControlEventSystemReserved // range reserved for internal framework use
UIControlEventAllEvents // 通知所有事件