diff --git a/Example/Modules/Platform/cocoa/SwiftUI_SPI.swiftinterface b/Example/Modules/Platform/cocoa/SwiftUI_SPI.swiftinterface index 20aaad6ce..e78fe8ef3 100644 --- a/Example/Modules/Platform/cocoa/SwiftUI_SPI.swiftinterface +++ b/Example/Modules/Platform/cocoa/SwiftUI_SPI.swiftinterface @@ -58,3 +58,8 @@ public final class ViewGraph { extension SwiftUI.View { public func variableBlur(maxRadius: CoreGraphics.CGFloat, mask: SwiftUI.Image, opaque: Swift.Bool = false) -> some SwiftUI.View } + +@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, visionOS 1.0, *) +extension SwiftUI.View { + public func avoidsOrphans(_ flag: Swift.Bool) -> some SwiftUI.View +} diff --git a/Example/OpenSwiftUIUITests/View/Text/TextUITests.swift b/Example/OpenSwiftUIUITests/View/Text/TextUITests.swift index eb0a7f99f..f56b72d13 100644 --- a/Example/OpenSwiftUIUITests/View/Text/TextUITests.swift +++ b/Example/OpenSwiftUIUITests/View/Text/TextUITests.swift @@ -23,4 +23,9 @@ struct TextUITests { func textHeight() { openSwiftUIAssertSnapshot(of: TextBackgroundHeightExample()) } + + @Test + func orphanAndPushOut() { + openSwiftUIAssertSnapshot(of: TextOrphanAndPushOutExample()) + } } diff --git a/Example/Shared/View/Text/TextOrphanAndPushOutExample.swift b/Example/Shared/View/Text/TextOrphanAndPushOutExample.swift new file mode 100644 index 000000000..8a12bf251 --- /dev/null +++ b/Example/Shared/View/Text/TextOrphanAndPushOutExample.swift @@ -0,0 +1,34 @@ +// +// TextOrphanAndPushOutExample.swift +// Shared + +#if OPENSWIFTUI +@_spi(Private) import OpenSwiftUI +#else +import SwiftUI_SPI +#endif + +struct TextOrphanAndPushOutExample: View { + private let text = "OpenSwiftUI 开发者" + private let width = 150.0 + + @ViewBuilder + private func textView(avoidsOrphans: Bool? = nil) -> some View { + if let avoidsOrphans { + Text(text) + .frame(width: width, alignment: .leading) + .avoidsOrphans(avoidsOrphans) + } else { + Text(text) + .frame(width: width, alignment: .leading) + } + } + + var body: some View { + VStack(alignment: .leading, spacing: 16) { + textView() + textView(avoidsOrphans: true) + textView(avoidsOrphans: false) + } + } +}