@@ -75,6 +75,46 @@ public struct FitReadableContentWidth<Content>: View where Content: View {
7575 }
7676}
7777
78+ /// This view makes its content `View` fit the layout margins guide width.
79+ ///
80+ /// - Note: This modifier is equivalent to calling ``.fitToLayoutMarginsWidth()`` on
81+ /// the content view.
82+ public struct FitLayoutMarginsWidth < Content> : View where Content: View {
83+ let alignment : Alignment
84+ let content : Content
85+
86+ /// Initialize some ``FitLayoutMarginsWidth`` view.
87+ ///
88+ /// - Parameters:
89+ /// - alignment: The `Alignment` to use when `content` is smaller than
90+ /// the layout margins guide width.
91+ /// - content: The view that should fit the layout margins guide width.
92+ public init (
93+ alignment: Alignment = . center,
94+ @ViewBuilder content: ( ) -> Content
95+ ) {
96+ self . alignment = alignment
97+ self . content = content ( )
98+ }
99+
100+ public var body : some View {
101+ InsetContent ( alignment: alignment, content: content)
102+ . measureLayoutMargins ( )
103+ }
104+
105+ private struct InsetContent : View {
106+ let alignment : Alignment
107+ let content : Content
108+ @Environment ( \. layoutMarginsInsets) var layoutMarginsInsets
109+ var body : some View {
110+ content
111+ . frame ( maxWidth: . infinity, alignment: alignment)
112+ . padding ( . leading, layoutMarginsInsets. leading)
113+ . padding ( . trailing, layoutMarginsInsets. trailing)
114+ }
115+ }
116+ }
117+
78118extension View {
79119 /// Use this modifier to make the view fit the readable content width.
80120 ///
@@ -86,6 +126,18 @@ extension View {
86126 public func fitToReadableContentWidth( alignment: Alignment = . center) -> some View {
87127 FitReadableContentWidth ( alignment: alignment) { self }
88128 }
129+
130+ /// Use this modifier to make the view fit the layout margins guide width.
131+ ///
132+ /// - Parameter alignment: The `Alignment` to use when the view is smaller than
133+ /// the readable content width.
134+ /// - Note: You don't have to wrap this view inside a ``WithLayoutMargins`` view.
135+ /// - Note: This modifier is equivalent to wrapping the view inside a
136+ /// ``FitLayoutMarginsWidth`` view.
137+ public func fitToLayoutMarginsWidth( alignment: Alignment = . center) -> some View {
138+ FitLayoutMarginsWidth ( alignment: alignment) { self }
139+ }
140+
89141
90142 /// Use this modifier to populate the ``layoutMarginsInsets`` and ``readableContentInsets``
91143 /// for the target view.
0 commit comments