1.初始化控件(按鈕控件)
方式1:
UIButton *etbtn1 = [UIButton alloc]initWithFrame:CGRectMake(x起點(diǎn), y起點(diǎn), 寬度, 高度);
方式2:(常用方式)
UIButton *etbtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTye | 按鈕風(fēng)格 |
---|---|
UIButtonTypeCustom | 自定義株茶,無(wú)風(fēng)格 |
UIButtonTypeRoundedRect | 白色圓角矩形,類似偏好設(shè)置表格單元或者地址簿卡片 |
UIButtonTypeDetailDisclosure | 藍(lán)色小箭頭按鈕志秃,主要做詳細(xì)說(shuō)明用 |
UIButtonTypeInfoDark | 白色背景下使用的深色圓圈信息按鈕 |
UIButtonTypeContactAdd | 藍(lán)色加號(hào)(+)按鈕产镐,可以放在任何文字旁 |
2.基本屬性及用法
- 設(shè)置UILable的frame ( 即顯示起點(diǎn)坐標(biāo)(x,y)和寬高(width,height) )
etbtn.frame = CGRectMake(<#x起點(diǎn)#>,<#y起點(diǎn)#>,<#寬度#>,<#高度#>);
- 設(shè)置背景顏色
etbtn.backgroundColor = [UIColor redColor];
- 設(shè)置標(biāo)簽(即tag值): 標(biāo)記當(dāng)前UIButton 對(duì)象,用于其它位置獲取改對(duì)象使用
etbtn1.tag = 100;
- 設(shè)置UIButton的標(biāo)題及不同狀態(tài)(正常/選中)下的顯示樣式
UIControlState狀態(tài)枚舉
/***************UIButton點(diǎn)擊狀態(tài)****************/
UIControlStateApplication;
UIControlStateDisabled; //禁用狀態(tài)
UIControlStateFocused;
UIControlStateReserved;
UIControlStateHighlighted; // 高亮狀態(tài)
UIControlStateSelected; // 選擇中狀態(tài)
UIControlStateNormal; // 正常顯示狀態(tài)
/*********************************************/
/****************************方法介紹************************************/
//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題內(nèi)容(默認(rèn)為空)
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//設(shè)置對(duì)應(yīng)狀態(tài)的按鈕的圖片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//設(shè)置對(duì)應(yīng)狀態(tài)的按鈕背景圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題陰影顏色 (在ios3.0使用扶关,配合titleShadowOffset一起使用)
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
/*************************屬性使用********************************/
//文字(正常/高亮 兩種狀態(tài))
[etbtn1 setTitle:@"正常狀態(tài)文字" forState:UIControlStateNormal];
[etbtn1 setTitle:@"點(diǎn)擊狀態(tài)文字" forState:UIControlStateHighlighted];
//文字顏色(正常/高亮 兩種狀態(tài))
[etbtn1 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[etbtn1 setTitleColor:[UIColor purpleColor] forState:UIControlStateHighlighted];
//顯示圖片(正常/高亮 兩種狀態(tài))
[etbtn1 setImage:[UIImage imageNamed:@"btn_normal.png"] forState:UIControlStateNormal];
[etbtn1 setImage:[UIImage imageNamed:@"btn_selected.png"] forState:UIControlStateHighlighted];
//顯示背景圖片(正常/高亮 兩種狀態(tài))
[etbtn1 setBackgroundImage:[UIImage imageNamed:@"btn_nomarl_Background"] forState:UIControlStateNormal];
[etbtn1 setBackgroundImage:[UIImage imageNamed:@"btn_selected_Background"] forState:UIControlStateHighlighted];
5 獲取及時(shí)屬性
屬性列表如下
/**************************************UIButton當(dāng)前狀態(tài)下的各種屬性(對(duì)象)*****************************************************************/
// currentTitle; //當(dāng)前狀態(tài)下標(biāo)題 normal/highlighted/selected/disabled. can return nil /
// currentTitleColor; //當(dāng)前狀態(tài)下標(biāo)題顏色 normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1) /
// currentImage; //當(dāng)前狀態(tài)下圖片 normal/highlighted/selected/disabled. can return nil /
// currentBackgroundImage; //當(dāng)前狀態(tài)下背景圖片 normal/highlighted/selected/disabled. can return nil /
/***************************************************************************************************************************************/
使用方法:
UIButton *etbtn = [UIButton buttonWithType:UIButtonTypeCustom];
etbtn.currentTitle; //當(dāng)前標(biāo)題,可以是正吵鹤疲或者點(diǎn)擊狀態(tài)
etbtn.currentTitleColor; //當(dāng)前標(biāo)題顏色
etbtn.currentImage; //當(dāng)前圖片
etbtn.currentBackgroundImage; //當(dāng)前背景圖片
6.添加點(diǎn)擊事件(或者說(shuō)按鈕觸發(fā)事件)
使用方法如下:
[etbtn1 addTarget:self
action:@selector(_efOnClick:withEvent:) //觸發(fā)的函數(shù)
forControlEvents:UIControlEventTouchUpInside];//設(shè)置出發(fā)觸發(fā)狀態(tài)
- (void)_efOnClick:(UIButton*)btn withEvent:(UIEvent*)event{
//觸發(fā)函數(shù)慧域,在里面進(jìn)行其它操作
//注意了!!!!!!!!!
//說(shuō)明:防止用戶惡意暴力點(diǎn)擊按鈕
UITouch *touch = [[event allTouches] anyObject];
if (touch.tapCount == 1) {//當(dāng)用戶瞬間點(diǎn)擊次數(shù)為1的時(shí)候觸發(fā)
}
}
Button的UIControlEvent觸發(fā)狀態(tài)
/********************************UIControlEvent狀態(tài)列表*************************************/
UIControlEventTouchDown ; // 用戶按下時(shí)觸發(fā)
UIControlEventTouchDownRepeat ; // 點(diǎn)擊次數(shù)大于1時(shí)觸發(fā)
UIControlEventTouchDragInside ; // 當(dāng)觸摸在控件內(nèi)拖動(dòng)時(shí)觸發(fā)
UIControlEventTouchDragOutside ; // 當(dāng)觸摸在控件之外拖動(dòng)時(shí)觸發(fā)
UIControlEventTouchDragEnter ; // 當(dāng)觸摸從控件外拖動(dòng)到內(nèi)部時(shí)
UIControlEventTouchDragExit ; // 當(dāng)觸摸從控件內(nèi)拖動(dòng)到外部時(shí)
UIControlEventTouchUpInside ; // 在控件內(nèi)觸摸抬起時(shí)
UIControlEventTouchUpOutside ; // 在控件外觸摸抬起時(shí)
UIControlEventTouchCancel ; // 觸摸取消事件,設(shè)備被鎖上或者電話呼叫打斷
UIControlEventValueChanged ; // 當(dāng)控件的值發(fā)生改變時(shí)
UIControlEventEditingDidBegin ; // 文本控件開始編輯時(shí)
UIControlEventEditingChanged ; // 文本控件的文本改變
UIControlEventEditingDidEnd ; // 文本控件結(jié)束編輯時(shí)
UIControlEventEditingDidEndOnExit ; // 文本控件內(nèi)通過(guò)按下回車鍵結(jié)束編輯時(shí)
UIControlEventAllTouchEvents ; // 所有觸摸事件
UIControlEventAllEditingEvents ; // 文本編輯的所有事件片拍,for UITextField
UIControlEventAllEvents ; // 所有事件
/****************************************************************************************/
- 特殊屬性
// 每邊(左,右妓肢,頂部和底部)可以有不同的值捌省。使用UIEdgeInsetsMake功能設(shè)置圖片和文字的位置(默認(rèn)為UIEdgeInsetsZero)
UIEdgeInsets contentEdgeInsets;
// 設(shè)置標(biāo)題、圖片的邊緣值(默認(rèn)UIEdgeInsetsZero)
UIEdgeInsets titleEdgeInsets;
BOOL reversesTitleShadowWhenHighlighted; // 決定是否點(diǎn)擊按鈕會(huì)導(dǎo)致其發(fā)光
BOOL adjustsImageWhenHighlighted; // 決定是否按鈕時(shí)碉钠,突出顯示圖像的變化纲缓。
BOOL adjustsImageWhenDisabled; // 決定是否形象的變化時(shí),該按鈕被禁用
BOOL showsTouchWhenHighlighted; // 決定是否點(diǎn)擊按鈕會(huì)導(dǎo)致其發(fā)光
- 最后需要將控件添加到父視圖上
3.擴(kuò)展類
- 延伸UIButton的熱感應(yīng)范圍(或者說(shuō)擴(kuò)展點(diǎn)擊范圍喊废,不改變?cè)瓉?lái)frame的大凶8摺)
- block方式調(diào)用與常規(guī)方式調(diào)用對(duì)比
//使用前提條件:導(dǎo)入U(xiǎn)IButton+Extern擴(kuò)展類
//擴(kuò)展點(diǎn)擊范圍
[self.evbtn setEnlargeEdgeWithTop:<#上線距離#>
right:<#向右距離#>
bottom:<#向下距離#>
left:<#向左距離#>];
//block與常規(guī)方式調(diào)用對(duì)比
/**************************常規(guī)方式*****************************/
UIButton *etbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[etbtn1 addTarget:self
action:@selector(_efOnClick:withEvent:) //觸發(fā)的函數(shù)
forControlEvents:UIControlEventTouchUpInside];//設(shè)置出發(fā)觸發(fā)狀態(tài)
- (void)_efOnClick:(UIButton*)btn withEvent:(UIEvent*)event{
//觸發(fā)函數(shù),在里面進(jìn)行其它操作
}
/**************************block方式*****************************/
UIButton *etbtn = [UIButton buttonWithType:UIButtonTypeCustom];
__block typeof(self) weakSelf = self;//注意:N劭辍9す搿!瓣蛀!避免循環(huán)引用造成內(nèi)測(cè)不及時(shí)釋放
[etbtn addBlockTarget:UIControlEventTouchUpInside block:^(UIEvent *touch) {
//觸發(fā)函數(shù)陆蟆,在里面進(jìn)行其它操作
//weakSelft方式調(diào)用
}];
UIButton+Extern .h文件
#import <UIKit/UIKit.h>
typedef void (^btnOnClick)(UIEvent*event);
@interface UIButton (Extern)
/**
* 擴(kuò)展Buttom的點(diǎn)擊范圍
*
* @param top top 方向延伸
* @param right right 方向延伸
* @param bottom bottom 方向延伸
* @param left left 方向延伸
*/
- (void) setEnlargeEdgeWithTop:(CGFloat)top
right:(CGFloat)right
bottom:(CGFloat)bottom
left:(CGFloat)left;
/**
* 使用Block語(yǔ)法塊,addTarget方法
*
* @param event events
* @param action block action
*/
- (void) addBlockTarget:(UIControlEvents)event
block:(btnOnClick)action;
@end
UIButton+Extern .m文件
#import "UIButton+Extern.h"
#import <objc/runtime.h>
@implementation UIButton (Extern)
#pragma mark 添加點(diǎn)擊范圍擴(kuò)展
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
- (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGRect) enlargedRect{
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
if (topEdge && rightEdge && bottomEdge && leftEdge){
return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
} else {
return self.bounds;
}
}
- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event{
CGRect rect = [self enlargedRect];
if (CGRectEqualToRect(rect, self.bounds)){
return [super hitTest:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? self : nil;
}
#pragma mark 添加addTarget Block方法
static char overviewKey;
- (void)addBlockTarget:(UIControlEvents)event block:(btnOnClick)action{
//用于給對(duì)象添加關(guān)聯(lián)對(duì)象,傳入 nil 則可以移除已有的關(guān)聯(lián)對(duì)象
objc_setAssociatedObject(self, &overviewKey, action, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(_efOnClick: event:) forControlEvents:event];
}
- (void)_efOnClick:(id)sender event:(UIEvent*)event{
btnOnClick block = (btnOnClick)objc_getAssociatedObject(self, &overviewKey);
if (block) {
block(event);
}
}
@end
4.引用其它人擴(kuò)展類
UIButton包含圖片和文字的同時(shí)惋增,自定義兩元素的布局位置
//使用前提條件:導(dǎo)入下面的Button+CenterImageAndTitle擴(kuò)展類
UIButton *etbtn = [UIButton buttonWithType:UIButtonTypeCustom];
/**
.
.
.
*/
[etbtn setTitle:@"顯示文字" forState:UIControlStateNormal];
[etbtn setImage:[UIImage imageNamed:@"btnImageNoamrl.png"] forState:UIControlStateNormal];
/*************************************使用方式如下5種****************************/
//1.系統(tǒng)默認(rèn)圖片在左叠殷,文字在右,間隔為0
[etbtn verticalCenterImageAndTitle:10.0f]; //2.上下居中诈皿,圖片在上林束,文字在下
[etbtn horizontalCenterTitleAndImage:50.0f]; //3.左右居中像棘,文字在左,圖片在右
[etbtn horizontalCenterImageAndTitle:50.0f]; //4.左右居中壶冒,圖片在左缕题,文字在右
[etbtn horizontalCenterTitleAndImageLeft:50.0f]; //5.文字居中,圖片在左邊
[etbtn horizontalCenterTitleAndImageRight:50.0f]; //6.文字居中依痊,圖片在右邊
.h文件
#import <UIKit/UIKit.h>
@interface UIButton (CenterImageAndTitle)
//上下居中避除,圖片在上,文字在下
- (void)verticalCenterImageAndTitle:(CGFloat)spacing;
- (void)verticalCenterImageAndTitle; //默認(rèn)6.0
//左右居中胸嘁,文字在左瓶摆,圖片在右
- (void)horizontalCenterTitleAndImage:(CGFloat)spacing;
- (void)horizontalCenterTitleAndImage; //默認(rèn)6.0
//左右居中,圖片在左性宏,文字在右
- (void)horizontalCenterImageAndTitle:(CGFloat)spacing;
- (void)horizontalCenterImageAndTitle; //默認(rèn)6.0
//文字居中群井,圖片在左邊
- (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing;
- (void)horizontalCenterTitleAndImageLeft; //默認(rèn)6.0
//文字居中,圖片在右邊
- (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing;
- (void)horizontalCenterTitleAndImageRight; //默認(rèn)6.0
@end
.m文件
#import "UIButton+CenterImageAndTitle.h"
@implementation UIButton (CenterImageAndTitle)
- (void)verticalCenterImageAndTitle:(CGFloat)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing/2), 0.0);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing/2), 0.0, 0.0, - titleSize.width);
}
- (void)verticalCenterImageAndTitle
{
const int DEFAULT_SPACING = 6.0f;
[self verticalCenterImageAndTitle:DEFAULT_SPACING];
}
- (void)horizontalCenterTitleAndImage:(CGFloat)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, imageSize.width + spacing/2);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + spacing/2, 0.0, - titleSize.width);
}
- (void)horizontalCenterTitleAndImage
{
const int DEFAULT_SPACING = 6.0f;
[self horizontalCenterTitleAndImage:DEFAULT_SPACING];
}
- (void)horizontalCenterImageAndTitle:(CGFloat)spacing;
{
// get the size of the elements here for readability
// CGSize imageSize = self.imageView.frame.size;
// CGSize titleSize = self.titleLabel.frame.size;
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, - spacing/2);
self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing/2, 0.0, 0.0);
}
- (void)horizontalCenterImageAndTitle;
{
const int DEFAULT_SPACING = 6.0f;
[self horizontalCenterImageAndTitle:DEFAULT_SPACING];
}
- (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing
{
// get the size of the elements here for readability
// CGSize imageSize = self.imageView.frame.size;
// CGSize titleSize = self.titleLabel.frame.size;
self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing, 0.0, 0.0);
}
- (void)horizontalCenterTitleAndImageLeft
{
const int DEFAULT_SPACING = 6.0f;
[self horizontalCenterTitleAndImageLeft:DEFAULT_SPACING];
}
- (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, 0.0);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + imageSize.width + spacing, 0.0, - titleSize.width);
}
- (void)horizontalCenterTitleAndImageRight
{
const int DEFAULT_SPACING = 6.0f;
[self horizontalCenterTitleAndImageRight:DEFAULT_SPACING];
}
@end