LNMultiLineStackView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // LNMultiLineStackView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/14.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNMultiLineStackView: UIStackView {
  11. var columns: Int = 0
  12. var itemDistribution: UIStackView.Distribution = .fillEqually
  13. var itemSpacing: CGFloat = 0
  14. private var lineViews: [UIStackView] = []
  15. private(set) var curItemViews: [UIView] = []
  16. private var emptyView: [UIView] = []
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. axis = .vertical
  20. }
  21. func update(_ itemViews: [UIView]) {
  22. arrangedSubviews.forEach {
  23. removeArrangedSubview($0)
  24. $0.removeFromSuperview()
  25. }
  26. lineViews.removeAll()
  27. curItemViews.removeAll()
  28. emptyView.removeAll()
  29. if itemViews.isEmpty { return }
  30. var line = buildLine()
  31. itemViews.forEach {
  32. if $0.isHidden {
  33. return
  34. }
  35. if line.arrangedSubviews.count == columns {
  36. line = buildLine()
  37. }
  38. line.addArrangedSubview($0)
  39. curItemViews.append($0)
  40. }
  41. fillLastLine()
  42. }
  43. func append(_ itemViews: [UIView]) {
  44. var last = lineViews.last ?? buildLine()
  45. emptyView.forEach {
  46. last.removeArrangedSubview($0)
  47. $0.removeFromSuperview()
  48. }
  49. itemViews.forEach {
  50. if $0.isHidden {
  51. return
  52. }
  53. if last.arrangedSubviews.count == columns {
  54. last = buildLine()
  55. }
  56. last.addArrangedSubview($0)
  57. curItemViews.append($0)
  58. }
  59. fillLastLine()
  60. }
  61. func remove(_ itemViews: [UIView]) {
  62. curItemViews.removeAll {
  63. itemViews.contains($0)
  64. }
  65. update(curItemViews)
  66. }
  67. required init(coder: NSCoder) {
  68. fatalError("init(coder:) has not been implemented")
  69. }
  70. }
  71. extension LNMultiLineStackView {
  72. private func fillLastLine() {
  73. if lineViews.isEmpty { return }
  74. guard let last = lineViews.last else { return }
  75. guard last.arrangedSubviews.count < columns else { return }
  76. for _ in last.arrangedSubviews.count..<columns {
  77. let empty = UIView()
  78. last.addArrangedSubview(empty)
  79. if itemDistribution == .equalSpacing
  80. || itemDistribution == .equalCentering {
  81. empty.snp.makeConstraints { make in
  82. make.width.equalTo(last.arrangedSubviews[0])
  83. }
  84. }
  85. emptyView.append(empty)
  86. }
  87. }
  88. private func buildLine() -> UIStackView {
  89. let stackView = UIStackView()
  90. stackView.axis = .horizontal
  91. stackView.distribution = itemDistribution
  92. stackView.spacing = itemSpacing
  93. lineViews.append(stackView)
  94. addArrangedSubview(stackView)
  95. return stackView
  96. }
  97. }
  98. #if DEBUG
  99. import SwiftUI
  100. struct LNMultiLineStackViewPreview: UIViewRepresentable {
  101. func makeUIView(context: Context) -> some UIView {
  102. let container = UIView()
  103. container.backgroundColor = .lightGray
  104. let view = LNMultiLineStackView()
  105. view.backgroundColor = .orange
  106. container.addSubview(view)
  107. view.snp.makeConstraints { make in
  108. make.leading.trailing.equalToSuperview()
  109. make.centerY.equalToSuperview()
  110. }
  111. return container
  112. }
  113. func updateUIView(_ uiView: UIViewType, context: Context) { }
  114. }
  115. #Preview(body: {
  116. LNMultiLineStackViewPreview()
  117. })
  118. #endif