SwiftUI 框架

在 SwiftUI App 中構建多個 Theme 一鍵簡單套用不同的 Theme

在這篇教學文章中,Pavlos 會利用一個只有一個 Text 的範例 App,帶大家為 SwiftUI App 建立多個 theme,讓 App 根據不同的 Theme 更改顏色和文本。快來一起動手實作,為你的 App 客製化自己的 Theme 吧!
在 SwiftUI App 中構建多個 Theme 一鍵簡單套用不同的 Theme
在 SwiftUI App 中構建多個 Theme 一鍵簡單套用不同的 Theme
In: SwiftUI 框架
本篇原文(標題:How to Create Multiple Themes in a SwiftUI Application)刊登於作者 Medium,由 Pavlos Simas 所著,並授權翻譯及轉載。

在這篇教學文章中,我們會看看如何為 SwiftUI App 建立多個 theme。

我們會利用一個只有一個 Text 的範例 App,它會根據不同的 Theme 更改顏色和文本。

建立 SwiftUI 專案

建立一個新專案,並選擇 SwiftUI 視圖。我們的範例 App 需要建立 4 個新檔案。

把它們命名為 ThemeManagerThemeBlueTheme、和 RedTheme。你的 File Explorer 看起來應該是這樣:

暫時不用修改 View 的 “Hello World” 文本,我們稍後會再處理。

現在,讓我們看看如何建立一個新的 Theme

首先,我們需要一個所有 Theme 都會實作的協定 (protocol),名為 Theme 協定。我們的 File 看起來應該是這樣的:

import Foundation
import SwiftUI

protocol Theme {
    var textColor: Color { set get }
    var welcomeText: String { set get }
}

範例的協定十分簡單,當中只有兩個屬性 (property),一個是 textColor,代表文本的顏色,另一個是 welcomeText,是 Text 的字串 (string)。

建立好協定之後,讓我們來建立 BlueThemeRedTheme 吧。以下是 BlueTheme 的程式碼:

import SwiftUI

final class BlueTheme: Theme {
    var textColor: Color = .blue
    var welcomeText: String = "I have the blue theme"
}

以下就是 RedTheme 的程式碼:

import SwiftUI

final class RedTheme: Theme {
    var textColor: Color = .red
    var welcomeText: String = "I have the red theme"
}

好了,現在我們建立了兩個 theme。如你所見,BlueTheme 顏色為藍色,文本是 I have the blue theme;而 RedTheme 顏色為紅色,文本是 I have the red theme

ContentView 套用 theme 之前,我們需要建立一個 Manager 來追蹤當前的 theme,讓 App 所有實例都可以應用它們。

讓我們調用它的管理器 ThemeManager

class ThemeManager {
    static let shared = ThemeManager()
    private var theme: Theme = RedTheme() // Default theme

    public func applyTheme(theme: Theme) {
        self.theme = theme
    }

    public func getTheme() -> Theme {
        return self.theme
    }
}

ThemeManager 是一個簡單的單例 (singleton),有 getapply theme功能。為方便起見,我們將預設主題設置為 RedTheme

現在,可以把 theme 套用到 ContentView 了:

import SwiftUI

struct ContentView: View {

    init() {
        self.applyBlueTheme()
    }

    var body: some View {
        let theme = ThemeManager.shared.getTheme()
        Text(theme.welcomeText)
            .foregroundColor(theme.textColor)
            .padding()
    }

    private func applyBlueTheme() {
        ThemeManager.shared.applyTheme(theme: BlueTheme())
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

init 方法內建立視圖之前,我們需要先調用 applyBlueTheme 函式,然後函式會調用 manager,並設置 BlueTheme 為當前的 theme。

之後,設置數值的步驟就十分簡單。

在構建視圖時,我們會從之前創建的 theme.welcomeText 中取得文本、並從 theme.textColor 中取得顏色。

如果我們想顯示紅色的文本,只要從 BlueTheme 轉到 RedTheme 就可以了!讓我們看看實作的結果吧:

這篇文章到此為止。謝謝你的閱讀。

本篇原文(標題:How to Create Multiple Themes in a SwiftUI Application)刊登於作者 Medium,由 Pavlos Simas 所著,並授權翻譯及轉載。
作者簡介:Pavlos Simas,iOS 開發者,對開發、行銷和金融非常有興趣。如果你有興趣閱讀我的其他文章,可以在 Medium 上訂閱。
譯者簡介:Kelly Chan-AppCoda 編輯小姐。

作者
AppCoda 編輯團隊
此文章為客座或轉載文章,由作者授權刊登,AppCoda編輯團隊編輯。有關文章詳情,請參考文首或文末的簡介。
評論
更多來自 AppCoda 中文版
如何在 SwiftUI App 中開發 Live Activities
SwiftUI 框架

如何在 SwiftUI App 中開發 Live Activities

Live Activities 首次於 iOS 16 推出,是 Apple 最令人興奮的更新之一,能讓 App 與使用者在即時互動上更有連結。它不再需要使用者不斷打開 App,Live Activities 可以讓資訊直接顯示在鎖定畫面和 Dynamic Island 上。
使用 Tool Calling 強化 Foundation Models 功能
AI

使用 Tool Calling 強化 Foundation Models 功能

在前幾篇教學中,我們介紹了 Foundation Models 在 iOS 26 中的運作方式,以及如何使用這個全新框架打造具備 AI 功能的應用。我們也介紹了 @Generable 巨集,它能輕鬆地將模型回應轉換為結構化的 Swift 類型。 現在,在這個 Foundation
活用 Foundation Models 的 @Generable 與 @Guide 製作測驗 App
AI

活用 Foundation Models 的 @Generable 與 @Guide 製作測驗 App

在前一篇教學中,我們介紹了 Foundation Models 框架,並示範了如何用它來進行基本的內容生成。那個過程相當簡單——你提供一個提示詞(prompt),等幾秒鐘,就能獲得自然語言的回應。在我們的範例中,我們建立了一個簡單的問答 App,讓使用者可以提問,App 則直接顯示生成的文字。 但如果回應變得更複雜——你需要把非結構化文字轉換為結構化的物件呢? 舉例來說,
很好! 你已成功註冊。
歡迎回來! 你已成功登入。
你已成功訂閱 AppCoda 中文版 電子報。
你的連結已失效。
成功! 請檢查你的電子郵件以獲取用於登入的連結。
好! 你的付費資料已更新。
你的付費方式並未更新。