键值对视图 (KeyValueView)

一个使用颜色和字符串的 KeyValue SwiftUI 视图

使用方法 (Usage)

import KeyValueView

示例 (Example)

public enum SomeColors: String, Hashable {
    case textColor, backgroundColor, iconColor
}

public enum SomeStrings: String, Hashable {
    case usageDescription
}

public struct SomeView<
    ColorDictionary: ColorProviding,
    StringDictionary: StringProviding
>: KeyValueView where ColorDictionary.ColorKey == SomeColors,
                    StringDictionary.StringKey == SomeStrings {
                    
    public var colorDictionary: ColorDictionary
    public var stringDictionary: StringDictionary
    
    public init(
        colorDictionary: ColorDictionary,
        stringDictionary: StringDictionary
    ) {
        self.colorDictionary = colorDictionary
        self.stringDictionary = stringDictionary
    }
    
    public var body: some View {
        Text(string(forKey: .usageDescription, defaultString: "👋"))
            .foregroundColor(color(forKey: .textColor, defaultColor: .white))
            .background(color(forKey: .backgroundColor, defaultColor: .clear))
    }
}

使用键值对视图 (Using a KeyValueView)

定义颜色和字符串 (Define Colors and Strings)

struct SomeColorDictionary: ColorProviding {
    var colors: [SomeColors : UIColor] = [
        .backgroundColor: .orange,
        .textColor: .blue
    ]
}

struct SomeStringDictionary: StringProviding {
    var strings: [SomeStrings : String] = [
        .usageDescription: "import KeyValueView"
    ]
}

创建一个键值对视图 (Create a KeyValueView)

SomeView(
    colorDictionary: SomeColorDictionary(),
    stringDictionary: SomeStringDictionary()
)