UIButton小結(jié)

前言

本來(lái)沒(méi)有打算寫這篇文章的, 主要是因?yàn)樵诠ぷ髦杏龅揭恍┩略儆?有UIButton的時(shí)候, 有些很基本的,系統(tǒng)API提供的都不知道, 例如 如何讓UIButton的文字居上,居左, 居右, 居下對(duì)其等一些基本點(diǎn), 為此我特地寫了一下UIButton小結(jié)

UIButton回顧

繼承關(guān)系

NSObject -> UIResponder -> UIView -> UIControl -> UIButton

API

初始化

遍歷構(gòu)造器

+ (instancetype)buttonWithType:(UIButtonType)buttonType;

button類型

 typedef NS_ENUM(NSInteger, UIButtonType) {
     UIButtonTypeCustom = 0,                        //自定義風(fēng)格
     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //系統(tǒng)樣式,從iOS7開(kāi)始使用
     UIButtonTypeDetailDisclosure,                  //藍(lán)色小箭頭按鈕,主要做詳細(xì)說(shuō)明用
     UIButtonTypeInfoLight,                         //亮色感嘆號(hào)
     UIButtonTypeInfoDark,                          //暗色感嘆號(hào)
     UIButtonTypeContactAdd,                        //十字加號(hào)按鈕
     UIButtonTypeRoundedRect = UIButtonTypeSystem,  //圓角矩形,從iOS7廢棄,iOS6中可以使用
 };

偏移量

內(nèi)容偏移量:正值表示間隔值,負(fù)值表示超出參照物的距離。UIEdgeInsetsMake(top, left, bottom, right)有四個(gè)值需要設(shè)置,分別距離上左下右邊的間隔。

// default is UIEdgeInsetsZero. On tvOS 10 or later, default is nonzero except for custom buttons.
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;

標(biāo)題偏移量:和圖片偏移量是相對(duì)的,比如:自定義一個(gè)按鈕實(shí)現(xiàn)的效果是圖片在左邊,標(biāo)題在右邊,可以用這個(gè)屬性,設(shè)置完標(biāo)題偏移量,圖片偏移量就是相對(duì)于標(biāo)題的

@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero

圖片偏移量

@property(nonatomic) UIEdgeInsets imageEdgeInsets;  

其他API

button的狀態(tài)為高亮?xí)r,文本的陰影會(huì)反轉(zhuǎn) 默認(rèn)是NO

@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;

button的狀態(tài)為高亮?xí)r,圖像變暗 默認(rèn)是YES

@property(nonatomic) BOOL adjustsImageWhenHighlighted;

button的狀態(tài)為禁用時(shí),圖像變暗。默認(rèn)是YES

@property(nonatomic) BOOL adjustsImageWhenDisabled;

button的狀態(tài)為高亮?xí)r,發(fā)光。默認(rèn)是NO

@property(nonatomic) BOOL showsTouchWhenHighlighted;

系統(tǒng)的一些樣式DetailDisclosure InfoLight InfoDark ContactAdd顏色會(huì)改變

@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0);

button的狀態(tài)。包括一些其他的控制的狀態(tài)

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,      //正常狀態(tài)
    UIControlStateHighlighted  = 1 << 0, //高亮狀態(tài)
    UIControlStateDisabled     = 1 << 1, //禁用狀態(tài)
    UIControlStateSelected     = 1 << 2, //選中狀態(tài)
    UIControlStateApplication  = 0x00FF0000,
    UIControlStateReserved     = 0xFF000000
};
// 設(shè)置標(biāo)題 default is nil. title is assumed to be single line
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;  

// 設(shè)置標(biāo)題顏色 default if nil. use opaque white
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;

// 設(shè)置標(biāo)題陰影顏色default is nil. use 50% black
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; 

// 設(shè)置圖片default is nil. 
should be same size if different for different states
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;  

// 設(shè)置背景圖片// default is nil
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; 

// 設(shè)置富文本標(biāo)題default is nil. title is assumed to be single line
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); 
// 返回不同狀態(tài)下標(biāo)題
- (nullable NSString *)titleForState:(UIControlState)state;

// 返回不同狀態(tài)下標(biāo)題顏色
- (nullable UIColor *)titleColorForState:(UIControlState)state;

// 返回不同狀態(tài)下標(biāo)題陰影顏色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;

// 返回不同狀態(tài)下圖片
- (nullable UIImage *)imageForState:(UIControlState)state;

// 返回不同狀態(tài)下背景圖片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;

// 返回不同狀態(tài)下富文本標(biāo)題
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

// button的當(dāng)前標(biāo)題。當(dāng)按鈕狀態(tài)改變時(shí)值自動(dòng)改變,可以做判斷,當(dāng)前標(biāo)題是全文則點(diǎn)擊展開(kāi)標(biāo)題設(shè)置為收起,當(dāng)前標(biāo)題是收起則點(diǎn)擊收起全文。
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;          

// 當(dāng)前標(biāo)題顏色default is white(1,1)
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;        

// 當(dāng)前狀態(tài)下標(biāo)題陰影顏色
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor; 
// 當(dāng)前狀態(tài)下圖片 切換不同圖片,比如做單選,多選可以使用。
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); 
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);
// 返回背景繪制區(qū)域
- (CGRect)backgroundRectForBounds:(CGRect)bounds;

// 返回內(nèi)容繪制區(qū)域。內(nèi)容區(qū)域是顯示圖片和標(biāo)題及他們特定對(duì)齊縮放等的范圍
- (CGRect)contentRectForBounds:(CGRect)bounds;

// 返回標(biāo)題的繪制區(qū)域
- (CGRect)titleRectForContentRect:(CGRect)contentRect;

// 返回圖片的繪制區(qū)域
- (CGRect)imageRectForContentRect:(CGRect)contentRect;

這個(gè)地方的API是UIControl的, 很多人并沒(méi)有在意這個(gè)類, 然后用一些很笨的手段去解決對(duì)其方式

// button 內(nèi)容垂直對(duì)其方式 default is center
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; 

// button 內(nèi)容水平對(duì)其方式 default is center
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 UIButton的父類是UIControl,UIControl的父類是UIView,UIView的父類是UI...
    guaker閱讀 2,879評(píng)論 1 9
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,324評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,657評(píng)論 4 61
  • 沒(méi)有敗給病魔,卻輸給了情緒??裨甑孟胍颜麄€(gè)醫(yī)院掀翻。醫(yī)生不再是救死扶傷的天使,而是宣判死刑的惡魔。三樓抽血四樓看...
    落雨叮當(dāng)想閱讀 366評(píng)論 0 0
  • 2016年3月4日清晨6點(diǎn)25分不到,出租車載著愛(ài)人,琳姐和我來(lái)到了去越南旅行集合地:北海地中海假日酒店。6點(diǎn)50...
    趙勝?gòu)?qiáng)閱讀 411評(píng)論 0 0

友情鏈接更多精彩內(nèi)容