Gravatar is a Swift package for requesting avatars from Gravatar.com.
The package provides a GravatarImage
SwiftUI view that enables you to easily display a person's Gravatar avatar in your app.
Use Swift Package Manager to add the Gravatar package to your Xcode project:
- Open your project in Xcode
- Select option Add Package Dependencies in the File menu
- Enter package URL
https://github.com/leomakkinje/Gravatar
in the search field - Click the Add Package button
- Select your project in the Add To Target popup menu
- Click the Add Package button
- Import the
Gravatar
package in your Swift file - Initialize a
Gravatar
instance with an email address - Initialize a
GravatarImage
view with aGravatar
instance
This example displays an avatar in an 80-pixel square without a border:
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com")
var body: some View {
GravatarImage(gravatar)
}
}
You initialize a Gravatar
instance with an email address, and optionally with custom values for the avatar's size, rating and default avatar.
// With default values
let gravatar = Gravatar("john.doe@example.com")
// With custom values
let gravatar = Gravatar("john.doe@example.com", size: 80, rating: .g, defaultAvatar: .mp, forceDefaultAvatar: false)
After initialization, the instance provides an .avatarURL
property, which is used by the GravatarImage
view for requesting the avatar's PNG image from Gravatar.com. Alternatively, you can use gravatar.avatarURL
with AsyncImage
if you want to create a custom image view.
You initialize a GravatarImage
view with a Gravatar
instance, and optionally with a custom style for the avatar's shape and border. The view's frame will be set to the size that is specified in the Gravatar
instance.
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com")
var body: some View {
GravatarImage(gravatar, style: .square)
}
}
If you require more view customizations than what GravatarImage
has to offer, you can initialize an AsyncImage
view with a gravatar.avatarURL
property and additional view modifiers. The gravatar.size
property can be used to set the view's frame size.
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com")
var body: some View {
AsyncImage(url: gravatar.avatarURL)
.frame(width: Double(gravatar.size), height: Double(gravatar.size))
// Add additional modifiers here
}
}
// Display the avatar in a 120-pixel circle with a border
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com", size: 120)
var body: some View {
GravatarImage(gravatar, style: .borderedCircle)
}
}
// Display the avatar in an 80-pixel rounded square without a border, and display
// the robohash avatar when an avatar for the specified email address does not exist.
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com", defaultAvatar: .robohash)
var body: some View {
GravatarImage(gravatar, style: .roundedSquare)
}
}
// Display the avatar in a 32-pixel square without a border, and always display
// the mystery person avatar, even when an avatar for the specified email address exists.
import Gravatar
import SwiftUI
struct ContentView: View {
let gravatar = Gravatar("john.doe@example.com", size: 32, forceDefaultAvatar: true)
var body: some View {
GravatarImage(gravatar)
}
}
Detailed documentation for Gravatar
and GravatarImage
is available in Xcode's Quick Help sidebar and Developer Documentation window, and online in the Swift Package Index.
This repository is open source and available under the MIT license. For more information, see the LICENSE file.
This repository is not affiliated with, sponsored by, or endorsed by Gravatar.com.