通过 JSON 文件创建 SwiftUI 用户界面。JSON 文件将包含用户界面的结构,程序将根据 JSON 文件创建用户界面。
警告
这是一个正在进行中的项目,尚未准备好用于生产环境。
请随时贡献代码、报告问题或请求功能。
使用 Swift Package Manager 安装
dependencies: [
.package(url: "https://github.com/0xWDG/DynamicUI.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "DynamicUI", package: "DynamicUI"),
]),
]
并导入它
import DynamicUI
import SwiftUI
import DynamicUI
struct ContentView: View {
let json = """
[
{
"type": "Text",
"title": "Wait, am i generating views from JSON?",
"modifiers": {"foregroundStyle":"red","opacity":0.6}
},
{
"type": "Button",
"title": "Click me",
"eventHandler": "customHandler"
},
{
"type": "Toggle",
"title": "Toggle me",
"identifier": "my.toggle.1"
}
]
""".data(using: .utf8)
var body: some View {
DynamicUI(
json: json,
callback: { component in
// This contains everything passed as a component (type, title, identifier, ...)
print(component)
}
)
}
}
在 Playground
目录下有一个 Xcode 项目用于构建 Playground 应用程序
请注意: 以 ⚠ 开头的项目可能会忽略传递的参数。带有删除线的项目尚未支持。
{
"type": "VStack",
"children": [ ]
}
{
"type": "HStack",
"children": [ ]
}
{
"type": "ZStack",
"children": [ ]
}
{
"type": "List",
"children": [ ]
}
{
"type": "ScrollView",
"children": [ ]
}
{
"type": "NavigationView",
"children": [ ]
}
{
"type": "Form",
"children": [ ]
}
{
"type": "Button",
"title": "Click me!"
}
{
"type": "Text",
"title": "..."
}
{
"type": "Image",
"imageURL": "systemName"
}
{
"type": "Divider"
}
{
"type": "Spacer"
}
{
"type": "Label",
"title": "..."
}
{
"type": "TextField",
"title": "...",
"defaultValue": "..."
}
{
"type": "SecureField",
"title": "...",
"defaultValue": "..."
}
{
"type": "TextEditor",
"title": "...",
"defaultValue": "..."
}
{
"type": "Toggle",
"title": "Turn me on!",
"defaultValue": true
}
{
"type": "Gauge",
"title": "...",
"defaultValue": 0.5
}
{
"type": "ProgressView",
"title": "...",
"value": 50,
"total": 100
}
{
"type": "Slider",
"title": "...",
"minLabel": "min",
"maxLabel": "max"
}
{
"type": "GroupBox",
"children": [ ]
}
{
"type": "DisclosureGroup",
"children": [ ]
}
{
"type": "HSplitView",
"children": [ ]
}
{
"type": "VSplitView",
"children": [ ]
}
{
"type": "Picker",
"title": "...",
"values": ["...", "..."]
}
{
"type": "NavigationSplitView",
"children": [ ]
}
{
"type": "TabView",
"children": [ ]
}
🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord