UIButton小结
前言
本來沒有打算寫這篇文章的, 主要是因為在工作中遇到一些同事再用 有UIButton的時候, 有些很基本的,系統(tǒng)API提供的都不知道, 例如 如何讓UIButton的文字居上,居左, 居右, 居下對其等一些基本點(diǎn), 為此我特地寫了一下UIButton小結(jié)
UIButton回顧
繼承關(guān)系
NSObject -> UIResponder -> UIView -> UIControl -> UIButton 復(fù)制代碼API
初始化
遍歷構(gòu)造器
+ (instancetype)buttonWithType:(UIButtonType)buttonType; 復(fù)制代碼button類型
typedef NS_ENUM(NSInteger, UIButtonType) {UIButtonTypeCustom = 0, //自定義風(fēng)格UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //系統(tǒng)樣式,從iOS7開始使用UIButtonTypeDetailDisclosure, //藍(lán)色小箭頭按鈕,主要做詳細(xì)說明用UIButtonTypeInfoLight, //亮色感嘆號UIButtonTypeInfoDark, //暗色感嘆號UIButtonTypeContactAdd, //十字加號按鈕UIButtonTypeRoundedRect = UIButtonTypeSystem, //圓角矩形,從iOS7廢棄,iOS6中可以使用}; 復(fù)制代碼偏移量
內(nèi)容偏移量:正值表示間隔值,負(fù)值表示超出參照物的距離。UIEdgeInsetsMake(top, left, bottom, right)有四個值需要設(shè)置,分別距離上左下右邊的間隔。
// default is UIEdgeInsetsZero. On tvOS 10 or later, default is nonzero except for custom buttons. @property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; 復(fù)制代碼標(biāo)題偏移量:和圖片偏移量是相對的,比如:自定義一個按鈕實現(xiàn)的效果是圖片在左邊,標(biāo)題在右邊,可以用這個屬性,設(shè)置完標(biāo)題偏移量,圖片偏移量就是相對于標(biāo)題的
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero 復(fù)制代碼圖片偏移量
@property(nonatomic) UIEdgeInsets imageEdgeInsets; 復(fù)制代碼其他API
button的狀態(tài)為高亮?xí)r,文本的陰影會反轉(zhuǎn) 默認(rèn)是NO
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; 復(fù)制代碼button的狀態(tài)為高亮?xí)r,圖像變暗 默認(rèn)是YES
@property(nonatomic) BOOL adjustsImageWhenHighlighted; 復(fù)制代碼button的狀態(tài)為禁用時,圖像變暗。默認(rèn)是YES
@property(nonatomic) BOOL adjustsImageWhenDisabled; 復(fù)制代碼button的狀態(tài)為高亮?xí)r,發(fā)光。默認(rèn)是NO
@property(nonatomic) BOOL showsTouchWhenHighlighted; 復(fù)制代碼系統(tǒng)的一些樣式DetailDisclosure InfoLight InfoDark ContactAdd顏色會改變
@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0); 復(fù)制代碼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 }; 復(fù)制代碼// 設(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); 復(fù)制代碼// 返回不同狀態(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);復(fù)制代碼// button的當(dāng)前標(biāo)題。當(dāng)按鈕狀態(tài)改變時值自動改變,可以做判斷,當(dāng)前標(biāo)題是全文則點(diǎn)擊展開標(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); 復(fù)制代碼@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); @property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0); 復(fù)制代碼// 返回背景繪制區(qū)域 - (CGRect)backgroundRectForBounds:(CGRect)bounds;// 返回內(nèi)容繪制區(qū)域。內(nèi)容區(qū)域是顯示圖片和標(biāo)題及他們特定對齊縮放等的范圍 - (CGRect)contentRectForBounds:(CGRect)bounds;// 返回標(biāo)題的繪制區(qū)域 - (CGRect)titleRectForContentRect:(CGRect)contentRect;// 返回圖片的繪制區(qū)域 - (CGRect)imageRectForContentRect:(CGRect)contentRect; 復(fù)制代碼這個地方的API是UIControl的, 很多人并沒有在意這個類, 然后用一些很笨的手段去解決對其方式
// button 內(nèi)容垂直對其方式 default is center @property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; // button 內(nèi)容水平對其方式 default is center @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; 復(fù)制代碼轉(zhuǎn)載于:https://juejin.im/post/5aa53fb86fb9a028dd4ddaf3
總結(jié)
以上是生活随笔為你收集整理的UIButton小结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。