LNAutoFillStackView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // LNAutoFillStackView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/12.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNAutoFillStackView: UIStackView {
  11. var itemSpacing: CGFloat = 0 {
  12. didSet {
  13. if oldValue != itemSpacing {
  14. sortItemViews()
  15. }
  16. }
  17. }
  18. private var lineViews: [UIStackView] = []
  19. private(set) var curItemViews: [UIView] = []
  20. override init(frame: CGRect) {
  21. super.init(frame: frame)
  22. axis = .vertical
  23. alignment = .leading
  24. }
  25. func update(_ itemViews: [UIView]) {
  26. arrangedSubviews.forEach {
  27. removeArrangedSubview($0)
  28. $0.removeFromSuperview()
  29. }
  30. lineViews.removeAll()
  31. curItemViews.removeAll()
  32. itemViews.forEach {
  33. $0.layoutIfNeeded()
  34. curItemViews.append($0)
  35. }
  36. sortItemViews()
  37. }
  38. func append(_ itemViews: [UIView]) {
  39. itemViews.forEach {
  40. $0.layoutIfNeeded()
  41. curItemViews.append($0)
  42. }
  43. sortItemViews()
  44. }
  45. func remove(_ itemViews: [UIView]) {
  46. curItemViews.removeAll {
  47. itemViews.contains($0)
  48. }
  49. sortItemViews()
  50. }
  51. override func layoutSubviews() {
  52. super.layoutSubviews()
  53. sortItemViews()
  54. }
  55. required init(coder: NSCoder) {
  56. fatalError("init(coder:) has not been implemented")
  57. }
  58. }
  59. extension LNAutoFillStackView {
  60. private func sortItemViews() {
  61. let maxWidth = bounds.width
  62. var lines: [[UIView]] = []
  63. var curLine: [UIView] = []
  64. var totalWidth = 0.0
  65. curItemViews.forEach {
  66. if curLine.isEmpty {
  67. curLine.append($0)
  68. totalWidth = $0.frame.width
  69. } else {
  70. if totalWidth + $0.bounds.width + itemSpacing > maxWidth {
  71. lines.append(curLine)
  72. curLine = []
  73. totalWidth = 0.0
  74. }
  75. if curLine.isEmpty {
  76. totalWidth = $0.bounds.width
  77. } else {
  78. totalWidth += $0.bounds.width + itemSpacing
  79. }
  80. curLine.append($0)
  81. }
  82. }
  83. lines.append(curLine)
  84. if lineViews.count != lines.count {
  85. lineViews.forEach {
  86. removeArrangedSubview($0)
  87. $0.removeFromSuperview()
  88. }
  89. lineViews.removeAll()
  90. for _ in 0..<lines.count {
  91. let lineView = buildLine()
  92. lineViews.append(lineView)
  93. addArrangedSubview(lineView)
  94. }
  95. }
  96. lineViews.forEach { line in
  97. line.arrangedSubviews.forEach {
  98. line.removeArrangedSubview($0)
  99. $0.removeFromSuperview()
  100. }
  101. }
  102. for (index, lineView) in lineViews.enumerated() {
  103. let views = lines[index]
  104. views.forEach {
  105. lineView.addArrangedSubview($0)
  106. }
  107. }
  108. }
  109. private func buildLine() -> UIStackView {
  110. let stackView = UIStackView()
  111. stackView.axis = .horizontal
  112. stackView.spacing = itemSpacing
  113. return stackView
  114. }
  115. }
  116. #if DEBUG
  117. import SwiftUI
  118. struct LNMultiAutoLineStackViewPreview: UIViewRepresentable {
  119. func makeUIView(context: Context) -> some UIView {
  120. let container = UIView()
  121. container.backgroundColor = .lightGray
  122. let view = LNAutoFillStackView()
  123. view.backgroundColor = .orange
  124. container.addSubview(view)
  125. view.snp.makeConstraints { make in
  126. make.leading.trailing.equalToSuperview()
  127. make.centerY.equalToSuperview()
  128. }
  129. return container
  130. }
  131. func updateUIView(_ uiView: UIViewType, context: Context) { }
  132. }
  133. #Preview(body: {
  134. LNMultiAutoLineStackViewPreview()
  135. })
  136. #endif