0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Qiita全国学生対抗戦Advent Calendar 2023

Day 8

【SwiftUI】箇条書きできるViewを作成する

Posted at

はじめに

箇条書きViewってありそうでないんですよね
そう思ったので作ってみました。

サンプルアプリ

スクリーンショット 2023-12-27 22.44.40.png

実装

import SwiftUI

struct BulletPointListView: View {
    let alignment: HorizontalAlignment
    let spacing: CGFloat?
    let bulletPointShape: BulletPointShape
    let items: [String]
    
    init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, bulletPointShape: BulletPointShape, items: String...) {
        self.alignment = alignment
        self.spacing = spacing
        self.bulletPointShape = bulletPointShape
        self.items = items
    }
    
    enum BulletPointShape: String {
        case circle = "circle"
        case circleFill = "circle.fill"
        case square = "square"
        case squareFill = "square.fill"
        case diamond = "diamond"
        case diamondFill = "diamond.fill"
    }

    var body: some View {
        VStack(alignment: alignment, spacing: spacing) {
            ForEach(items, id: \.self) { item in
                HStack(alignment: .top, spacing: 5) {
                    Text(Image(systemName: bulletPointShape.rawValue))
                        .scaleEffect(0.5) // BulletPointのサイズを調整
                    
                    Text(item)
                }
            }
        }
    }
}

使い方

import SwiftUI

struct ContentView: View {
    var body: some View {
        BulletPointListView(
            bulletPointShape: .circle,
            items: "あいうえお", "かきくけこ", "さしすせそ", "たちつてと"
        )
    }
}

おわり

本当はこんな感じで作りたかったのですが、技術不足で実現できませんでした。

BulletPointListView(bulletPointShape: .circle) {
    Text("あいうえお")
    Text("かきくけこ")
    Text("さしすせそ")
    Text("たちつてと")
}

こんな感じでできませんかね?

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?