iOS-UIButton 全面解析

02.jpg

字數1443 閱讀548 評論5 喜歡36
UIButton 的全面解析
建議收藏,用到的時候來這里一查就都明白了

初始化Button 不用alloca init 的方法 用便利構造器初始化
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, -自定義風格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
UIButtonTypeDetailDisclosure, -藍色小箭頭按鈕,主要做詳細說明
UIButtonTypeInfoLight, -亮色感嘆號
UIButtonTypeInfoDark, -暗色感嘆號
UIButtonTypeContactAdd, -十字加號按鈕
UIButtonTypeRoundedRect = UIButtonTypeSystem, -圓角矩形
};
//設置button frmae
button.frame = CGRectMake(100, 100, 100, 100);
//設置button 背景顏色
button.backgroundColor = [UIColor orangeColor];
//添加button的標題
[button setTitle:@"登陸" forState:UIControlStateNormal];
//設置標題的顏色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設置標題字體的大小
button.titleLabel.font = [UIFont systemFontOfSize:20];
//設置button的背景圖片
[button setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
//獲取指定狀態下的背景圖片
UIImage *tempImage = [button imageForState:UIControlStateNormal];
//設置前景圖片 前景圖片必須是鏤空圖,或者是線條勾勒的圖片
[button setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
//設置陰影顏色
[button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];

//button 一些其他屬性
@property(nonatomic) UIEdgeInsets contentEdgeInsets; 內容內距離
@property(nonatomic) UIEdgeInsets titleEdgeInsets; 標題內距離
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; 標題的陰影改變時,按鈕是否高亮顯示。默認為NO
@property(nonatomic) UIEdgeInsets imageEdgeInsets; 圖片內邊距
@property(nonatomic) BOOL adjustsImageWhenHighlighted;按鈕高亮的情況下,圖像的顏色是否要加深一點。默認是YES
@property(nonatomic) BOOL adjustsImageWhenDisabled; 按鈕禁用的情況下,圖像的顏色是否要加深一點。默認是YES
@property(nonatomic) BOOL showsTouchWhenHighlighted; 按下按鈕是否會發光 默認是NO
@property(nonatomic,readonly) UIButtonType buttonType; button的類型

設置button某個狀態的標題

  • (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
    例: [button setTitle:@"開燈" forState:UIControlStateNormal];

設置button某個狀態的標題顏色

  • (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state
    例:[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

設置button某個狀態陰影的標題顏色

  • (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state
    例:[button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];

設置button某個狀態圖片

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

設置button 某個狀態背景圖片

  • (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil

設置button 某個狀態下的富文本標題

  • (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line

返回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);

/* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/
以下是幾種狀態
enum {
UIControlStateNormal = 0, 常規狀態顯現
UIControlStateHighlighted = 1 << 0, 高亮狀態顯現
UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現
UIControlStateSelected = 1 << 2, 選中狀態
UIControlStateApplication = 0x00FF0000, 當應用程序標志時
UIControlStateReserved = 0xFF000000 為內部框架預留,可以不管他
};

獲取按鈕當前標題
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

獲取按鈕當前標題顏色
@property(nonatomic,readonly,strong) UIColor *currentTitleColor;

獲取按鈕當前陰影標題顏色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;

獲取按鈕當前按鈕內圖像
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage;

獲取按鈕當前標題背景圖片
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage;

獲取按鈕當前標題富文本
@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;
    指定內容邊界
  • (CGRect)contentRectForBounds:(CGRect)bounds;
    指定標題邊界
  • (CGRect)titleRectForContentRect:(CGRect)contentRect;
    指定圖片邊界
  • (CGRect)imageRectForContentRect:(CGRect)contentRect;

示例:

  • (CGRect)imageRectForContentRect:(CGRect)bounds{
    return CGRectMake(0.0, 0.0, 44, 44);
    }
    @end

//給按鈕添加點擊事件
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

枚舉值:

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             // 通知所有事件
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,363評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,497評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,305評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,962評論 1 311
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,727評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,193評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,257評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,411評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,945評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,777評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,978評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,519評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,216評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,642評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,878評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,657評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,960評論 2 373

推薦閱讀更多精彩內容

  • 前言:UI控件整理之UIButton 一、顯示圖片(復選框) UIButton *button = [UIButt...
    心如止水的魚閱讀 289評論 0 0
  • 一個UIButton的實例變量, 使一個按鈕(button)在觸摸屏上生效。一個按鈕監聽觸摸事件,當被點擊時,給目...
    wushuputi閱讀 1,515評論 0 1
  • 概述 UIButton的父類是UIControl,UIControl的父類是UIView,UIView的父類是UI...
    guaker閱讀 2,812評論 1 9
  • 一、UITabBarController以其相關控件之間的關系 @interface UITabBarContro...
    西門淋雨閱讀 3,100評論 0 1
  • 在宿舍大家常感嘆的一句話就是,“哎,一天就過去···哎,一個星期又過去了···哎,一個月又過去了”,不知不覺...
    小風竹閱讀 352評論 0 0