iOS開發UI篇-常用控件詳解(UIButton)

一. 常用屬性


  • IButton內有兩個控件titleLabelimageView,可以用來顯示一個文本和圖片,給UIButton設置了title和image后,圖片在左邊,文本在圖片右邊顯示,它們兩個做為一個整體依賴于button的contentHorizontalAlignment居左居右或居中顯示。
    1.當button.width < image.width時,只顯示被壓縮后的圖片,圖片是按fillXY的方式壓縮。
    2.當button.width > image.width,且 button.width < (image.width + text.width)時,圖片正常顯示,文本被壓縮。
    3.當button.width > (image.width + text.width),兩者并列默認居中顯示,可通過button的屬性contentHorizontalAlignment改變水平對齊方式。
    想改變兩個子控件的顯示位置,可以分別通過setTitleEdgeInsetssetImageEdgeInsets來實現。需要注意的是,對titleLabelimageView設置偏移,是針對它當前的位置起作用的,并不是針對它距離button邊框的距離的。

二. 基本屬性


  • contentEdgeInsets: default is UIEdgeInsetsZero.設置內容四邊距,默認邊距為0

  • titleEdgeInsets: default is UIEdgeInsetsZero,文字內邊距。默認邊距為0.

  • reversesTitleShadowWhenHighlighted: default is NO. if YES, shadow reverses to shift between engrave and emboss appearance。默認狀態是 NO,按鈕高亮時,陰影出現在相反位置。

  • imageEdgeInsets: default is UIEdgeInsetsZero。圖片內邊距,默認邊距為0;

  • adjustsImageWhenHighlighted : default is YES. if YES, image is drawn darker when highlighted(pressed),當按鈕高亮時是否調整圖片。默認是 YES

  • adjustsImageWhenDisabled:default is YES. if YES, image is drawn lighter when disabled.默認是 yes,如果是 YES,圖形繪制較輕時禁用

  • showsTouchWhenHighlighted:default is NO. if YES, show a simple feedback (currently a glow) while highlighted。默認是 NO,如果是 YES,在高亮期間顯示一個簡單的反饋(當前發光).如果沒有高亮狀態,不做任何反饋。

  • tintColor: The tintColor is inherited through the superview hierarchy. See UIView for more information. 這個屬性是從 UIView 繼承過來的,更多解釋信息見 UIView.該屬性不應用于UIButtonTypeCustom.如果Custom 類型的按鈕需要使用該屬性,應該重寫該屬性的 set方法。

  • buttonType:按鈕樣式屬性,取值枚舉類型

UIButtonTypeCustom:無類型,按鈕的內容需要自定義
UIButtonTypeDetailDisclosure:
UIButtonTypeInfoLight:
UIButtonTypeInfoDark:
UIButtonTypeContactAdd:

  • currentTitle:normal/highlighted/selected/disabled. can return nil。獲取當前的文本。

  • currentTitleColor:normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)。當前文本顏色,默認白色。

  • currentTitleShadowColor:normal/highlighted/selected/disabled.當前狀態下的文本陰影顏色

  • currentImage:normal/highlighted/selected/disabled. can return nil。當前狀態下的圖片。

  • currentBackgroundImage:normal/highlighted/selected/disabled. can return nil。當前狀態下的背景圖片

  • currentAttributedTitle:normal/highlighted/selected/disabled. can return nil。當前狀態下的文本屬性。

  • titleLabel:文本標簽屬性

  • imageView:圖片屬性

三. 常用方法


由于按鈕是有狀態的,比如:未點擊狀態,高亮狀態,選中狀態,不可用狀態等。所以按鈕所提供的方法當中很多是根據按鈕狀態來設置的。

normal(普通狀態)

默認情況(Default)
對應的枚舉常量:UIControlStateNormal

highlighted(高亮狀態)
按鈕被按下去的時候(手指還未松開)
對應的枚舉常量:UIControlStateHighlighted

disabled(失效狀態,不可用狀態)
如果enabled屬性為NO,就是處于disable狀態,代表按鈕不可以被點擊
對應的枚舉常量:UIControlStateDisabled

  • buttonWithType:類方法,創建按鈕對象時,直接指定按鈕類型。取值為枚舉。

  • setTitle:forState: 根據按鈕狀態設置文字。

  • setTitleColor:forState: 根據按鈕狀態設置文字顏色,默認白色。

  • setTitleShadowColor: forState: 設置文字陰影顏色,默認半透明黑色

  • setImage: forState: : 根據按鈕狀態設置圖片

  • setBackgroundImage: forState: 根據按鈕狀態設置背景圖片

  • setAttributedTitle: forState : 根據按鈕狀態設置文本屬性內容(包括文字大小,顏色等)。假設文字是單行的

  • titleForState:根據按鈕狀態獲取文本

  • titleColorForState: 根據按鈕狀態獲取文本顏色

  • titleShadowColorForState: 根據按鈕狀態獲取文本陰影顏色

  • imageForState: 根據按鈕狀態獲取圖片

  • backgroundImageForState: 根據按鈕狀態獲取背景圖片

  • attributedTitleForState: 根據按鈕狀態獲取文本屬性

  • backgroundRectForBounds::自定義按鈕時,可以更改背景圖片在按鈕當中的位置

  • contentRectForBounds:自定義按鈕時,可以更改整個內容在按鈕當中的位置

  • titleRectForContentRect: 自定義按鈕時,可以更改Label在按鈕當中的位置

  • imageRectForContentRect: 自定義按鈕時,可以更改圖片在按鈕當中的位置

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

推薦閱讀更多精彩內容