TagCloudView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import SwiftUI
  2. struct TagCloudView: View {
  3. var tags: [String]
  4. @State private var totalHeight
  5. // = CGFloat.zero // << variant for ScrollView/List
  6. = CGFloat.infinity // << variant for VStack
  7. var body: some View {
  8. VStack {
  9. GeometryReader { geometry in
  10. self.generateContent(in: geometry)
  11. }
  12. }
  13. // .frame(height: totalHeight)// << variant for ScrollView/List
  14. .frame(maxHeight: totalHeight) // << variant for VStack
  15. }
  16. private func generateContent(in g: GeometryProxy) -> some View {
  17. var width = CGFloat.zero
  18. var height = CGFloat.zero
  19. return ZStack(alignment: .topLeading) {
  20. ForEach(self.tags, id: \.self) { tag in
  21. self.item(for: tag)
  22. .padding([.horizontal, .vertical], 2)
  23. .alignmentGuide(.leading, computeValue: { d in
  24. if abs(width - d.width) > g.size.width
  25. {
  26. width = 0
  27. height -= d.height
  28. }
  29. let result = width
  30. if tag == self.tags.last! {
  31. width = 0 // last item
  32. } else {
  33. width -= d.width
  34. }
  35. return result
  36. })
  37. .alignmentGuide(.top, computeValue: { _ in
  38. let result = height
  39. if tag == self.tags.last! {
  40. height = 0 // last item
  41. }
  42. return result
  43. })
  44. }
  45. }.background(viewHeightReader($totalHeight))
  46. }
  47. private func item(for textTag: String) -> some View {
  48. var colorOfTag: Color {
  49. if textTag.contains("Not Floating") {
  50. return .loopYellow } else {
  51. switch textTag {
  52. case "Floating Carbs:":
  53. return .loopPink
  54. case "autoISF":
  55. return .zt
  56. case "SMB Delivery Ratio:":
  57. return .uam
  58. case "Parabolic Fit":
  59. return .loopRed
  60. case "Autosens":
  61. return .loopGreen
  62. case "Standard":
  63. return .darkerBlue
  64. case "TDD":
  65. return .loopYellow
  66. default:
  67. return .insulin
  68. }
  69. }
  70. }
  71. return ZStack { Text(textTag)
  72. .padding(.vertical, 2)
  73. .padding(.horizontal, 4)
  74. .font(.caption)
  75. .background(colorOfTag.opacity(0.8))
  76. .foregroundColor(Color.white)
  77. .cornerRadius(5) }
  78. }
  79. private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
  80. GeometryReader { geometry -> Color in
  81. let rect = geometry.frame(in: .local)
  82. DispatchQueue.main.async {
  83. binding.wrappedValue = rect.size.height
  84. }
  85. return .clear
  86. }
  87. }
  88. }
  89. struct TestTagCloudView: View {
  90. var body: some View {
  91. VStack {
  92. Text("Header").font(.largeTitle)
  93. TagCloudView(tags: ["Ninetendo", "XBox", "PlayStation", "PlayStation 2", "PlayStation 3", "PlayStation 4"])
  94. Text("Some other text")
  95. Divider()
  96. Text("Some other cloud")
  97. TagCloudView(tags: ["Apple", "Google", "Amazon", "Microsoft", "Oracle", "Facebook"])
  98. }
  99. }
  100. }