1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  Untitled.swift
//  LiveProject
//
//  Created by 倪路朋 on 6/26/25.
//
import SwiftUI
 
struct FlowLayout: Layout {
    var spacing: CGFloat = 8
    var lineSpacing: CGFloat = 8
 
    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
        var width: CGFloat = 0
        var height: CGFloat = 0
        var currentLineWidth: CGFloat = 0
        var currentLineHeight: CGFloat = 0
        let maxWidth = proposal.width ?? .infinity
 
        for view in subviews {
            let size = view.sizeThatFits(.unspecified)
            if currentLineWidth + size.width > maxWidth {
                width = max(width, currentLineWidth)
                height += currentLineHeight + lineSpacing
                currentLineWidth = size.width
                currentLineHeight = size.height
            } else {
                currentLineWidth += size.width + spacing
                currentLineHeight = max(currentLineHeight, size.height)
            }
        }
 
        width = max(width, currentLineWidth)
        height += currentLineHeight
 
        return CGSize(width: width, height: height)
    }
 
    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
        var x: CGFloat = 0
        var y: CGFloat = 0
        var lineHeight: CGFloat = 0
 
        for view in subviews {
            let size = view.sizeThatFits(.unspecified)
 
            if x + size.width > bounds.width {
                x = 0
                y += lineHeight + lineSpacing
                lineHeight = 0
            }
 
            view.place(
                at: CGPoint(x: bounds.minX + x, y: bounds.minY + y),
                proposal: ProposedViewSize(width: size.width, height: size.height)
            )
 
            x += size.width + spacing
            lineHeight = max(lineHeight, size.height)
        }
    }
}
// MARK: - 使用示例
 
struct FlowLayoutExample: View {
    let tags = [
        "Swift"
    ]
    
    @State private var newTag = ""
    @State private var customTags = ["自定义标签1", "自定义标签2"]
    
    var body: some View {
        VStack{
            VStack(spacing: 20) {
                FlowLayout(){
                    
                        ForEach(tags, id: \.self) { item in
                            Text(item)
                                .padding(.horizontal, 12)
                                .padding(.vertical, 6)
                                .background(Color.blue.opacity(0.2))
                                .cornerRadius(8)
                        }
                }.frame(alignment:.leading)
                .background(Color.red)
            }
            .frame(maxWidth: .infinity,alignment:.leading)
        }
        .background(Color.black)
    }
    
    private func addTag() {
        withAnimation {
            customTags.append(newTag)
            newTag = ""
        }
    }
}
 
/// 标签视图
struct TagView: View {
    let text: String
    
    var body: some View {
        Text(text)
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(Color.blue.opacity(0.2))
            .foregroundColor(.blue)
            .cornerRadius(10)
            .overlay(
                RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.blue, lineWidth: 1)
            )
            .fixedSize(horizontal: true, vertical: false) // 确保文本不被截断
    }
}
 
// MARK: - 预览
 
struct FlowLayout_Previews: PreviewProvider {
    static var previews: some View {
        FlowLayoutExample()
    }
}