SectionHeader.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // SectionHeader.swift
  3. // LoopKitUI
  4. //
  5. // Created by Nathaniel Hamming on 2020-02-20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct SectionHeader: View {
  10. var label: String
  11. var style: Style
  12. public enum Style {
  13. case regular
  14. case tight
  15. }
  16. public init(label: String, style: Style = .default) {
  17. self.label = label
  18. self.style = style
  19. }
  20. public var body: some View {
  21. if #available(iOSApplicationExtension 14.0, *) {
  22. // iOS 14 puts section headers in all-caps by default. This un-does that.
  23. content.textCase(nil)
  24. } else {
  25. content
  26. }
  27. }
  28. @ViewBuilder private var content: some View {
  29. Text(label)
  30. .font(.headline)
  31. .foregroundColor(.primary)
  32. .padding(.leading, style == .tight ? -10 : 0)
  33. }
  34. }
  35. public extension SectionHeader.Style {
  36. static let `default`: SectionHeader.Style = {
  37. if #available(iOSApplicationExtension 14.0, *) {
  38. return .regular
  39. } else {
  40. return .tight
  41. }
  42. }()
  43. }
  44. struct SectionHeader_Previews: PreviewProvider {
  45. static var previews: some View {
  46. SectionHeader(label: "Header Label")
  47. }
  48. }