UIButton小结
生活随笔
收集整理的這篇文章主要介紹了
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; 復制代碼轉載于:https://juejin.im/post/5aa53fb86fb9a028dd4ddaf3
總結
以上是生活随笔為你收集整理的UIButton小结的全部內容,希望文章能夠幫你解決所遇到的問題。