iOS 15 更容易客製的 UIButton — plain,gray,tinted & filled 四種樣式

iOS 15 的 UIButton 進化了 ! 現在它有 Plain,Gray,Tinted & Filled 四種樣式可選擇,可以做更多的客製,比方圖片的位置,圓角的弧度,圖片維持比例,圓形 button,搭配 AttributedString 調整字型等。

接下來讓我們一步步從 Interface Builder 認識這些新的設定欄位吧。

Objects Library 的新 button

從 Objects Library 可看到現在有六種 button 可選擇,包含了 Button,Gray Button,Tinted Button,Filled Button,Pull Down Button & Pop Up Button,待會我們將介紹前四種 button。

Button

首先讓我們從最基本的 Button 開始吧,它預設為 plain 樣式,主要用於呈現無背景的按鈕。

設定標題

設定副標題(subtitle)

從 Alignment 設定標題跟副標題的對齊

從 Title Padding 設定標題跟副標題的間距

多行文字

顯示多行文字也不是問題。

從 Image 設定和文字並排的圖片

使用 SF Symbol。

使用 assets 的圖片。

值得注意的,圖片顯示時會維持原本的大小,因此如果圖片太大,圖片將超出 button 的框框。若是希望圖片依 button 尺寸調整大小,請將圖片設成背景圖片,相關說明等下會提到。

只有圖沒有字也可以。

從 Foreground 設定圖文的顏色

從 Symbol Scale 設定 SF Symbol 的大小

從 Render Mode 設定 SF Symbol 的配色模式

在此我們選擇 Palette,因此有 Primary,Secondary,Tertiary 三種可設定的顏色。

SF Symbol 的配色模式可參考以下連結。

從 Placement 設定圖片的位置

圖片預設在文字的左邊,我們可從 Placement 調整它的位置。

圖片在 trailing。

圖片在 bottom。

從 Padding 設定圖文的間距

從 Background Configuration 的 Background 設定背景

選擇 Custom 後可設定相關欄位。

如下圖所示,我們在 Fill 設定黃色的背景,Corner Radius 設定圓角弧度,Stroke 設定邊框顏色,Width 設定邊框寬度。

Outset 設定邊框跟 button 的間距。

Image 設定背景圖片。

從 Background Configuration 的 Corner Style 選擇 Apple 設計的圓角

我們也可以從 Corner Style 選擇 Apple 設計的圓角。

以下為 Small,Medium,Large,Capsule 四種圓角的設計。

採用以上四種圓角時,button 將如下圖所示,自動依據大小調整圓角的弧度。

至於 Fixed & Dynamic,Fixed 代表固定的圓角弧度,Dynamic 則會依據 dynamic type 調整圓角弧度。

實現圖片維持比例的 button & 圓形 button

相關說明可參考以下連結。

從 Background Configuration 的 Content Insets 設定圖文和 button 邊邊的額外距離

圖文預設是置中,如下圖所示,選擇 Custom 後可設定相關欄位。我們將 Leading 設為 30,Top 設為 20,因此圖文將往右下移動。

Background Configuration 的 Shows Activity Indicator

顯示 loading 圖示,App 執行時 button 將有 loading 轉轉轉的動畫。

設定 button 的尺寸為 Apple 設計的尺寸

切換到 Size inspector。

從 Size 欄位選擇 Apple 設計的尺寸

下圖依序為 Small & Large 的尺寸,可看到 Large 的圖文也會比 Small 大。

調整文字的字型和大小

  • 從 Interface Builder
  • 從程式
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

let button = UIButton(type: .system)
var config = UIButton.Configuration.plain()

var title = AttributedString("Login")
title.foregroundColor = UIColor.red
title.font = UIFont(name: "AmericanTypewriter-Bold", size: 30)
config.attributedTitle = title

var subtitle = AttributedString("welcome")
subtitle.foregroundColor = UIColor.blue
subtitle.font = UIFont(name: "AmericanTypewriter-Bold", size: 20)
config.attributedSubtitle = subtitle
button.configuration = config
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}

說明

利用 UIButton 的 property configuration 設定 button 的樣式,它的型別是 UIButton.Configuration,在此我們產生樣式 plain 的 configuration,然後從 attributedTitle & attributedSubtitle 設定文字的樣式。attributedTitle & attributedSubtitle 的型別是 AttributedString,因此可以設定文字的相關屬性,比方字型,大小,顏色等。

Gray Button,Tinted Button & Filled Button

聊完了最基本的 plain Button,接著讓我們瞧瞧 Gray Button,Tinted Button & Filled Button。

除了從 Objects library 選擇喜歡的 button 樣式,我們也可以從 button 的 Style 欄位設定。

在 iOS 15,button 有 Plain,Gray,Tinted & Filled 四種樣式。

我們在 plain button 做的相關設定,都可以在 Gray,Tinted & Filled button 設定,接著讓我們看看它們特別的地方。

Gray Button

預設有圓角,背景顏色是灰色。

從 Background 可修改背景顏色。

Tinted Button

預設有圓角,圖文跟背景顏色由 Tint 控制。

將 Tint 設為紅色。

Filled Button

預設有圓角,背景顏色由 Tint 控制,圖文預設為白色。

將 Tint 設為紅色。

值得注意的,Gray Button,Tinted Button & Filled Button 的背景顏色除了從前面介紹的 Background Configuration 設定,也可以從下圖的 Background 設定。

顯示 X 圖示的 close button

修改 AttributedString 的文字內容

Style Default : 舊版 button 的設定方法

Plain,Gray,Tinted & Filled button 都是透過 UIButton 的 property configuration 設定樣式,不過我們也可以不搭配 configuration,採用舊版 button 的設定方法。

在 Style 欄位選擇 Default 將變成採用舊版的設定方法。

iOS 15 除了讓 UIButton 變得更容易客製,也多了一些包含特別功能的 button,像是 Toggle Button,Pull Down Button & Pull Up Button,這些我們將在之後的文章另外介紹。

iOS 15 自動更新選取狀態的 toggle button

從程式設定 iOS UIKit Button 的文字,圖片 & 樣式

iOS 的選單(menu)按鈕 — Pull Down Button & Pop Up Button

參考連結

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com