[Xcode/SwiftUI] 絵文字リストを作る

実装

import SwiftUI

struct HomeView: View {
    @State private var selectedEmoji: String = ""
    private let emojis = [
        "😀", "😍", "🎉", "🎈", "🌟", "🚀", "🍔", "🍦", "🎸", "🎮", "⚽", "📸",
        "🐶", "🐱", "🐻", "🐼", "🦁", "🐮", "🐷", "🐸", "🦉", "🐙", "🦄", "🐴",
        "🚗", "🚲", "✈️", "🚁", "🛴", "🚆",
        "🍕", "🌮", "🍣", "🍩", "🍭", "🍺", "🍸", "🎂", "🍪", "🥂"
    ]

    var body: some View {
        VStack {
            Text("Emoji Picker")
                .font(.system(size: 28))
                .padding()
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum: 60))], spacing: 20) {
                    ForEach(emojis, id: \.self) { emoji in
                        EmojiButton(emoji: emoji, selectedEmoji: $selectedEmoji)
                    }
                }
                .padding()
            }
            Text("Selected Emoji: \(selectedEmoji)")
                .font(.system(size: 28))
        }
    }
}

struct EmojiButton: View {
    var emoji: String
    @Binding var selectedEmoji: String

    var body: some View {
        Button(action: {
            selectedEmoji = emoji
        }) {
            Text(emoji)
                .font(.system(size: 30))
                .frame(width: 60, height: 60)
                .background(selectedEmoji == emoji ? Color.blue : Color.brown)
                .cornerRadius(10)
                .foregroundColor(.white)
        }
    }
}

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!