1. 程式人生 > >ios開發UI篇--UIButton

ios開發UI篇--UIButton

named per ron 多點 orf -- int www done

概述

  • UIButton 是執行自定義代碼以響應用戶交互的控件。
  • UIButton 其實包含 UIImageViewUILabel 兩個控件,UIButton 繼承於 UIControl,所以有 addtarget 監聽事件

屬性和方法

初始化Button不用alloc init方法,使用便利構造器方法

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

UIButton的類型如下

UIButton類型說明
UIButtonTypeCustom 沒有按鈕樣式,一般設置該樣式,根據需要自定義
UIButtonTypeSystem 系統樣式按鈕,例如導航欄和工具欄中顯示的按鈕。
UIButtonTypeDetailDisclosure 詳細披露按鈕。
UIButtonTypeInfoLight 具有淺色背景的信息按鈕。
UIButtonTypeInfoDark 信息按鈕有一個黑暗的背景。
UIButtonTypeContactAdd 聯系人添加按鈕。
UIButtonTypePlain 沒有模糊背景視圖的標準系統按鈕。
UIButtonTypeRoundedRect 已經廢棄,使用UIButtonTypeSystem代替

UIButton的狀態如下

UIButton的狀態說明
UIControlStateNormal 控件的正常狀態或默認狀態 - 即已啟用但未選中或高亮顯示。
UIControlStateHighlighted 突出顯示的控制狀態。按鈕處於選中狀態時的狀態
UIControlStateDisabled 一個控件的禁用狀態。
UIControlStateSelected 選擇一個控件的狀態。
UIControlStateFocused 集中控制狀態。
UIControlStateApplication 額外的控制狀態標誌可用於應用程序使用。
UIControlStateReserved 控制狀態標誌保留給內部框架使用。

設置frame

[btn setFrame:CGRectMake(100, 100, 100, 30)];

設置某一狀態下的標題

[btn setTitle:@"測試" forState:(UIControlStateNormal)];

設置某一狀態下的標題顏色

[btn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

設置某一狀態下的陰影顏色

[btn setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];

設置某一狀態下的背景顏色

[btn setBackgroundColor:[UIColor blackColor]];

設置標題字體大小

 btn.titleLabel.font = [UIFont systemFontOfSize:30];

設置某一狀態下的背景圖片(背景圖片顯示在其標題和前景圖像後面。)

[btn setBackgroundImage:[UIImage imageNamed:@"登錄logo"] forState:(UIControlStateNormal)];

設置某一狀態下的前景圖片

[btn setImage:[UIImage imageNamed:@"驗證碼"] forState:(UIControlStateNormal)];

設置標題內邊距

btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);   top, left, bottom, right;
技術分享圖片

設置圖片內邊距

btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

設置內容內邊距

btn.contentEdgeInsets = UIEdgeInsetsMake(10, -30, 10, 10);

標題的陰影改變時,按鈕是否高亮顯示。默認為NO

btn.reversesTitleShadowWhenHighlighted = YES;

按鈕高亮的情況下,圖像的顏色是否要加深一點。默認是YES

btn.adjustsImageWhenHighlighted = YES;

按鈕禁用的情況下,圖像的顏色是否要加深一點。默認是YES

btn.adjustsImageWhenDisabled = YES;

按下按鈕是否會發光 默認是NO

btn.showsTouchWhenHighlighted = NO;

返回button 某個狀態下的標題

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

返回button 某個狀態下的標題顏色

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

返回button 某個狀態下的陰影標題顏色

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

返回button 某個狀態下的圖片

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

返回button 某個狀態下的背景圖片

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

返回button 某個狀態下的富文本標題

- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

獲取按鈕當前標題

NSString *title = btn.currentTitle;

獲取按鈕當前標題顏色

UIColor *color = btn.currentTitleColor;

獲取按鈕當前陰影標題顏色

UIColor *shandowColor = btn.currentTitleShadowColor;

獲取按鈕當前按鈕內圖像

UIImage *image = btn.currentImage;

獲取按鈕當前標題背景圖片

UIImage *backgroundImage = btn.currentBackgroundImage;

獲取按鈕當前標題富文本

NSAttributedString *aString = btn.currentAttributedTitle;

指定背景邊界

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

指定內容邊界

- (CGRect)contentRectForBounds:(CGRect)bounds;

指定標題邊界

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

指定圖片邊界

- (CGRect)imageRectForContentRect:(CGRect)contentRect;

給按鈕添加點擊事件

[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

UIButton點擊事件如下

UIButton點擊事件說明
UIControlEventTouchDown 單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat 多點觸摸按下事件,點觸計數大於1:用戶按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside 當一次觸摸在控件窗口內拖動時。
UIControlEventTouchDragOutside 當一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter 當一次觸摸從控件窗口之外拖動到內部時
UIControlEventTouchDragExit 當一次觸摸從控件窗口內部拖動到外部時。
UIControlEventTouchUpInside 所有在控件之內觸摸擡起事件
UIControlEventTouchUpOutside 所有在控件之外觸摸擡起事件(點觸必須開始與控件內部才會發送通知)。
UIControlEventTouchCancel 所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。
UIControlEventValueChanged 當控件的值發生改變時,發送通知。用於滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。
UIControlEventEditingDidBegin 當文本控件中開始編輯時發送通知
UIControlEventEditingChanged 當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd 當文本控件中編輯結束時發送通知。
UIControlEventEditingDidEndOnExit 當文本控件內通過按下回車鍵(或等價行為)結束編輯時,發送通知。
UIControlEventAllTouchEvents 通知所有觸摸事件
UIControlEventAllEditingEvents 通知所有關於文本編輯的事件。
UIControlEventApplicationReserved range available for application use
UIControlEventSystemReserved range reserved for internal framework use
UIControlEventAllEvents 通知所有事件


作者:coder小鵬

ios開發UI篇--UIButton