Name Avatar with Random Color
Initials avatar with deterministic or randomized gradient backgrounds.
SwiftUI Implementation
Pure Vanilla Swift • No DependenciesnameAvatarRandomColor.swift
import SwiftUI
struct UseNameAvatarRandomColor: View {
let name: String = "Diane"
var body: some View {
VStack(alignment: .center, spacing: 8) {
NameAvatarRandomColor(name: name)
Text(name)
.font(.headline)
}
}
}
#Preview {
UseNameAvatarRandomColor()
}
struct NameAvatarRandomColor: View {
let name: String
var body: some View {
Circle()
.fill(Color.random)
.frame(width: 64, height: 64, alignment: .center)
.shadow(radius: 3)
.overlay {
Text(String(name.first!))
.font(.system(size: 38, weight: .heavy, design: .serif))
.foregroundStyle(.background)
}
}
}
#Preview {
NameAvatarRandomColor(name: "Diane")
}
private extension Color {
static var random: Color {
var generator: RandomNumberGenerator = SystemRandomNumberGenerator()
return random(using: &generator)
}
static func random(using generator: inout RandomNumberGenerator) -> Color {
let red = Double.random(in: 0..<1, using: &generator)
let green = Double.random(in: 0..<1, using: &generator)
let blue = Double.random(in: 0..<1, using: &generator)
return Color(red: red, green: green, blue: blue)
}
}Ready to copy and paste directly into any Xcode project running iOS 16.0+.
Recommended Use Cases
- App settings, preferences, and account configuration screens
- Dashboard overviews, stats tracking, and metric highlights
Component Details
- Category
- Avatars & User Profiles
- iOS Compatibility
- iOS 16.0+
- Xcode Version
- Xcode 15+
- Platform
- iOS
- License Tier
- Free
- Themes & Contexts
- settingsdashboardaccount
C
Compot for iOS
Native SwiftUI playground app
Preview all 130+ components interactively on your device and copy code straight to Xcode with 1 tap.
Get Compot on the App StoreRelated SwiftUI Components
More components in Avatars & User Profiles and related app architectures.
ButtonsiOS 16.0+
Bouncy Button Icon
Interactive button featuring fluid spring animations and smooth scale feedback upon tap.
View Component & Code
ButtonsiOS 16.0+
Button with Repeat Behavior
Stepper-like button with continuous press-and-hold repeating action for counters and volume adjustment.
View Component & Code
ButtonsiOS 16.0+
Button for Copying
Smart copy button that temporarily switches icon and text to "Copied!" with tactile haptic feedback.
View Component & Code