SnappTheming

Static Badge Static Badge

SnappTheming 是一个 Swift 框架,旨在简化将动态设计主题集成到 iOS 应用程序中的过程。

通过利用 JSON 声明,该框架允许开发人员轻松提取和应用各种主题元素,例如颜色、字体、渐变和形状样式,直接应用到他们的应用程序的用户界面中。

目录

此存储库包含 SnappTheming 框架以及一个示例 Xcode 项目,用于演示其功能。

文档

该软件包的文档可以在这里找到

教程

了解使用 SwiftUI 创建您的第一个 SnappTheming 主题、定义颜色和样式以及管理多个主题以实现无缝用户切换的基本知识。按照教程进行实践,有效地为您的项目设置主题。可以在这里找到

快速开始

安装

使用 Xcode

  1. 导航到您的项目设置。
  2. 选择“Package Dependencies”(包依赖项)选项。
  3. 使用搜索功能来定位存储库:https://github.com/Snapp-Mobile/SnappTheming
  4. 选择“SnappTheming”包,然后选择“Add Package”(添加包)以将其合并到您的项目中。

使用 Swift Package Manager

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .package(url: "https://github.com/Snapp-Mobile/SnappTheming", from: "0.1.0"),
    ],
    targets: [
        .target(
            name: "YourPackage",
            dependencies: ["SnappTheming"]
        )
    ]
)

用法

在 SwiftUI 应用程序中

import OSLog
import SnappTheming
import SwiftUI

@main
struct STTestApp: App {
    @State var declaration: SnappThemingDeclaration?

    // Discover more about the JSON Schema at 
    // https://ios-theming.snappmobile.io/documentation/snapptheming/jsonschema
    private let json = """
        {
            "colors": {
                "textPrimary": "#1a1a1a",
            },
            "images": {
                "globe": "system:globe"
            },
            "metrics": {
                "label": 16.0,
                "icon": 24
            },
            "fonts": {
                "label": {
                    "postScriptName": "Arial-BoldMT"
                }
            },
            "typography": {
                "title": {
                    "font": "$fonts/label",
                    "fontSize": "$metrics/label"
                }
            }
        }
        """

    init() {
        let configuration = SnappThemingParserConfiguration(themeName: "Light")
        guard let declaration = try? SnappThemingParser.parse(from: json, using: configuration) else {
            os_log(.error, "Error loading theme")
            return
        }

        if !declaration.fontInformation.isEmpty {
            let fontManager = SnappThemingFontManagerDefault(
                themeCacheRootURL: configuration.themeCacheRootURL,
                themeName: configuration.themeName
            )
            fontManager.registerFonts(declaration.fontInformation)
        }
        _declaration = .init(initialValue: declaration)
    }

    var body: some Scene {
        WindowGroup {
            if let declaration {
                VStack {
                    HStack(alignment: .center) {
                        declaration.images.globe
                            .resizable()
                            .frame(maxWidth: declaration.metrics.icon, maxHeight: declaration.metrics.icon)

                        Text("Praise Kier.")
                            .font(declaration.typography.title)
                    }
                }
                .foregroundStyle(declaration.colors.textPrimary)
            } else {
                Text("Unable to load the theme")
                    .bold()
            }
        }
    }
}