VerticalAlignment.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. /// Alignment of items layed out along the vertical axis.
  2. public enum VerticalAlignment: Sendable {
  3. /// Top alignment.
  4. case top
  5. /// Center alignment.
  6. case center
  7. /// Bottom alignment.
  8. case bottom
  9. /// Converts this value to a ``StackAlignment``.
  10. var asStackAlignment: StackAlignment {
  11. switch self {
  12. case .top:
  13. .leading
  14. case .center:
  15. .center
  16. case .bottom:
  17. .trailing
  18. }
  19. }
  20. /// Gets the position of a child of a given height in a frame of a given
  21. /// height using the alignment.
  22. /// - Parameter childHeight: The height of the child.
  23. /// - Parameter frameHeight: The height of the frame.
  24. /// - Returns: The position of the child.
  25. func position(ofChild childHeight: Double, in frameHeight: Double) -> Double {
  26. switch self {
  27. case .top: 0
  28. case .center: (frameHeight - childHeight) / 2
  29. case .bottom: frameHeight - childHeight
  30. }
  31. }
  32. }