TagCloudView.swift 3.7 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. switch textTag {
  50. case textTag where textTag.contains("Not Floating"):
  51. return .loopYellow
  52. case textTag where textTag.contains("Floating Carbs"):
  53. return .loopPink
  54. case textTag where textTag.contains("autoISF"):
  55. return .zt
  56. case textTag where textTag.contains("SMB Delivery Ratio:"):
  57. return .uam
  58. case textTag where textTag.contains("Parabolic Fit"):
  59. return .loopRed
  60. case textTag where textTag.contains("TDD:"),
  61. textTag where textTag.contains("formula."),
  62. textTag where textTag.contains("AF:"),
  63. textTag where textTag.contains("Dynamic "):
  64. return .zt
  65. default:
  66. return .insulin
  67. }
  68. }
  69. return ZStack { Text(textTag)
  70. .padding(.vertical, 2)
  71. .padding(.horizontal, 4)
  72. .font(.subheadline)
  73. .background(colorOfTag.opacity(0.8))
  74. .foregroundColor(Color.white)
  75. .cornerRadius(5) }
  76. }
  77. private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
  78. GeometryReader { geometry -> Color in
  79. let rect = geometry.frame(in: .local)
  80. DispatchQueue.main.async {
  81. binding.wrappedValue = rect.size.height
  82. }
  83. return .clear
  84. }
  85. }
  86. }
  87. struct TestTagCloudView: View {
  88. var body: some View {
  89. VStack {
  90. Text("Header").font(.largeTitle)
  91. TagCloudView(tags: ["Ninetendo", "XBox", "PlayStation", "PlayStation 2", "PlayStation 3", "PlayStation 4"])
  92. Text("Some other text")
  93. Divider()
  94. Text("Some other cloud")
  95. TagCloudView(tags: ["Apple", "Google", "Amazon", "Microsoft", "Oracle", "Facebook"])
  96. }
  97. }
  98. }