UI--UIButton

前言:UI控件整理之UIButton

一、顯示圖片(復(fù)選框)


顯示圖片

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 15, 60,? 30, 30);

[button setImage:[UIImage imageNamed:@"icon_weixuanzhong-1"] forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:@"icon_xuanzhong-1"] forState:UIControlStateSelected];

[button setBackgroundColor:[UIColor whiteColor]];

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

[self.view addSubview:button];

二、顯示文字


顯示文字

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(30, 60,? [UIScreen mainScreen].bounds.size.width - 60, 30);

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

[button setTitle:@"UIButton" forState:UIControlStateNormal];

button.titleLabel.font = [UIFont fontWithName:@"IowanOldStyle-Bold" size:15];

[button setBackgroundColor:[UIColor whiteColor]];

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

[self.view addSubview:button];

三婚瓜、部分常用屬性解析

1埂伦、初始化Button(枚舉值)

//初始化Button 不用alloca init 的方法 用便利構(gòu)造器初始化

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

UIButtonTypeCustom // -自定義風(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 //-圓角矩形

2朝卒、//設(shè)置button的背景圖片

[button setBackgroundImage:[UIImage imageNamed:@"bg.png"] forState:UIControlStateNormal];

3耍群、//設(shè)置前景圖片 前景圖片必須是鏤空?qǐng)D木西,或者是線條勾勒的圖片

[button setImage:[UIImage imageNamed:@"qj.png"] forState:UIControlStateNormal];

4畴栖、狀態(tài)(forState)

/* forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)*/

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ù)留,可以不管他

5八千、獲取按鈕當(dāng)前標(biāo)題

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

6吗讶、獲取按鈕當(dāng)前按鈕內(nèi)圖像

@property(nullable, nonatomic,readonly,strong) UIImage? *currentImage;

7、按鈕點(diǎn)擊事件(枚舉值)

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)觸必須開始與控件內(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)文本控件中開始編輯時(shí)發(fā)送通知

UIControlEventEditingChanged? ? ? ? // 當(dāng)文本控件中的文本被改變時(shí)發(fā)送通知。

UIControlEventEditingDidEnd? ? ? ? // 當(dāng)文本控件中編輯結(jié)束時(shí)發(fā)送通知六剥。

UIControlEventEditingDidEndOnExit? // 當(dāng)文本控件內(nèi)通過(guò)按下回車鍵(或等價(jià)行為)結(jié)束編輯時(shí)晚顷,發(fā)送通知。

UIControlEventAllTouchEvents? ? ? ? // 通知所有觸摸事件疗疟。

UIControlEventAllEditingEvents? ? ? // 通知所有關(guān)于文本編輯的事件该默。

UIControlEventApplicationReserved? // range available for application use

UIControlEventSystemReserved? ? ? ? // range reserved for internal framework use

UIControlEventAllEvents? ? ? ? ? ? // 通知所有事件

8、標(biāo)題內(nèi)距離

@property(nonatomic) UIEdgeInsets titleEdgeInsets;

9策彤、圖片內(nèi)邊距

@property(nonatomic) UIEdgeInsets imageEdgeInsets;

四栓袖、常用拓展(系統(tǒng)屬性排列)

@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的類型

指定背景邊界

- (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);

}

獲取按鈕當(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);

設(shè)置button某個(gè)狀態(tài)的標(biāo)題

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

例: [button setTitle:@"開燈" 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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市恕洲,隨后出現(xiàn)的幾起案子塔橡,更是在濱河造成了極大的恐慌梅割,老刑警劉巖霜第,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異户辞,居然都是意外死亡泌类,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)刃榨,“玉大人弹砚,你說(shuō)我怎么就攤上這事∈嘞#” “怎么了桌吃?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)苞轿。 經(jīng)常有香客問(wèn)我茅诱,道長(zhǎng),這世上最難降的妖魔是什么搬卒? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任瑟俭,我火速辦了婚禮,結(jié)果婚禮上契邀,老公的妹妹穿的比我還像新娘摆寄。我一直安慰自己,他們只是感情好坯门,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布微饥。 她就那樣靜靜地躺著,像睡著了一般田盈。 火紅的嫁衣襯著肌膚如雪畜号。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天允瞧,我揣著相機(jī)與錄音简软,去河邊找鬼。 笑死述暂,一個(gè)胖子當(dāng)著我的面吹牛痹升,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播畦韭,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼疼蛾,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了艺配?” 一聲冷哼從身側(cè)響起察郁,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎转唉,沒想到半個(gè)月后皮钠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赠法,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年麦轰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡款侵,死狀恐怖末荐,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情新锈,我是刑警寧澤甲脏,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站妹笆,受9級(jí)特大地震影響剃幌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜晾浴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一负乡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧脊凰,春花似錦抖棘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至帕胆,卻和暖如春朝捆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背懒豹。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工芙盘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人脸秽。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓儒老,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親记餐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子驮樊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 字?jǐn)?shù)1443 閱讀548 評(píng)論5 喜歡36UIButton 的全面解析建議收藏,用到的時(shí)候來(lái)這里一查就都明白了 初...
    xiao小馬哥閱讀 1,213評(píng)論 0 1
  • 一個(gè)UIButton的實(shí)例變量片酝, 使一個(gè)按鈕(button)在觸摸屏上生效囚衔。一個(gè)按鈕監(jiān)聽觸摸事件,當(dāng)被點(diǎn)擊時(shí)雕沿,給目...
    wushuputi閱讀 1,505評(píng)論 0 1
  • 概述 UIButton的父類是UIControl练湿,UIControl的父類是UIView,UIView的父類是UI...
    guaker閱讀 2,800評(píng)論 1 9
  • 一晦炊、UITabBarController以其相關(guān)控件之間的關(guān)系 @interface UITabBarContro...
    西門淋雨閱讀 3,085評(píng)論 0 1
  • 需要改進(jìn)的地方: 第一: 跟叔叔談話時(shí) 要顯現(xiàn)出自己的專業(yè)性鞠鲜,確定要的就一定要用,不能模棱兩可断国,這樣自己的專業(yè)性就...
    Janey袁敏閱讀 139評(píng)論 0 0