UIButton小結

前言

本來沒有打算寫這篇文章的, 主要是因為在工作中遇到一些同事再用 有UIButton的時候, 有些很基本的,系統API提供的都不知道, 例如 如何讓UIButton的文字居上,居左, 居右, 居下對其等一些基本點, 為此我特地寫了一下UIButton小結

UIButton回顧

繼承關系

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

API

初始化

遍歷構造器

+ (instancetype)buttonWithType:(UIButtonType)buttonType;

button類型

 typedef NS_ENUM(NSInteger, UIButtonType) {
     UIButtonTypeCustom = 0,                        //自定義風格
     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //系統樣式,從iOS7開始使用
     UIButtonTypeDetailDisclosure,                  //藍色小箭頭按鈕,主要做詳細說明用
     UIButtonTypeInfoLight,                         //亮色感嘆號
     UIButtonTypeInfoDark,                          //暗色感嘆號
     UIButtonTypeContactAdd,                        //十字加號按鈕
     UIButtonTypeRoundedRect = UIButtonTypeSystem,  //圓角矩形,從iOS7廢棄,iOS6中可以使用
 };

偏移量

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

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

標題偏移量:和圖片偏移量是相對的,比如:自定義一個按鈕實現的效果是圖片在左邊,標題在右邊,可以用這個屬性,設置完標題偏移量,圖片偏移量就是相對于標題的

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

圖片偏移量

@property(nonatomic) UIEdgeInsets imageEdgeInsets;  

其他API

button的狀態為高亮時,文本的陰影會反轉 默認是NO

@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;

button的狀態為高亮時,圖像變暗 默認是YES

@property(nonatomic) BOOL adjustsImageWhenHighlighted;

button的狀態為禁用時,圖像變暗。默認是YES

@property(nonatomic) BOOL adjustsImageWhenDisabled;

button的狀態為高亮時,發光。默認是NO

@property(nonatomic) BOOL showsTouchWhenHighlighted;

系統的一些樣式DetailDisclosure InfoLight InfoDark ContactAdd顏色會改變

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

button的狀態。包括一些其他的控制的狀態

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

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

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

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

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

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

// 返回不同狀態下標題顏色
- (nullable UIColor *)titleColorForState:(UIControlState)state;

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

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

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

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

// button的當前標題。當按鈕狀態改變時值自動改變,可以做判斷,當前標題是全文則點擊展開標題設置為收起,當前標題是收起則點擊收起全文。
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;          

// 當前標題顏色default is white(1,1)
@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 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);
// 返回背景繪制區域
- (CGRect)backgroundRectForBounds:(CGRect)bounds;

// 返回內容繪制區域。內容區域是顯示圖片和標題及他們特定對齊縮放等的范圍
- (CGRect)contentRectForBounds:(CGRect)bounds;

// 返回標題的繪制區域
- (CGRect)titleRectForContentRect:(CGRect)contentRect;

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

這個地方的API是UIControl的, 很多人并沒有在意這個類, 然后用一些很笨的手段去解決對其方式

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

// button 內容水平對其方式 default is center
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述 UIButton的父類是UIControl,UIControl的父類是UIView,UIView的父類是UI...
    guaker閱讀 2,817評論 1 9
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,381評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,245評論 4 61
  • 沒有敗給病魔,卻輸給了情緒。狂躁得想要把整個醫院掀翻。醫生不再是救死扶傷的天使,而是宣判死刑的惡魔。三樓抽血四樓看...
    落雨叮當想閱讀 305評論 0 0
  • 2016年3月4日清晨6點25分不到,出租車載著愛人,琳姐和我來到了去越南旅行集合地:北海地中海假日酒店。6點50...
    趙勝強閱讀 359評論 0 0