Swiftpack.co - Package - liang2kl/AvatarX

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

liang2kl/AvatarX

Generate GitHub-style pixel avatars, with SwiftUI.

AvatarX

Generate GitHub-style pixel avatars, with SwiftUI.

Usage

To create an avatar view, simply initialize an Avatar instance with a hashable value and the configuration specifying colors and the resolution:

Avatar(
    configuration: AvatarConfiguration(colors: [.white, .blue, .green], resolution: 6), 
    value: 0
)

You can also specify whether to apply a symmetric style in the configuration, which is true by default:

public struct AvatarConfiguration {
    ...
    public var symmetric: Bool
    ...
}

Customization

You can customize AvatarX by implementing wrappers with a Hashable generic type, or a concrete type conforming to Hashable.

For example, to apply padding around the avatar using one of the colors:

struct AvatarWrapper<HashableValue>: View where HashableValue: Hashable {
    var colors: [Color]
    var resolution: Int
    var symmetric: Bool = true
    var padding: CGFloat = 0
    var value: HashableValue
    
    var body: some View {
        Avatar(
            configuration: AvatarConfiguration(
                colors: colors,
                resolution: resolution,
                symmetric: symmetric
            ),
            value: value
        )
        .padding(padding)
        .background(colors.count >= 1 ? colors[0] : nil)
    }
}

Github

link
Stars: 1
Last commit: 1 week ago