I am attempting to achieve a layout within an HStack
that contains two views: A Text
view that may or may not contain multiple lines of text, and a Button
. These are aligned with the top edge of the containing HStack
.
For the purpose of this question, the Button
could be any kind of View
.
I want the height of the Button
to be equal to the height of the first line of the text contained within the Text
element.
This is easy when the Text
contains a single line – there are obvious ways to make two views equally tall. But it’s unclear to me how to do this when the Text
contains multiple lines of text when the size of the text could differ based on accessibility settings, etc.
I know how to do this in UIKit, but am unsure of how to do it with SwiftUI. Does anybody have any ideas? Thanks!
I’ve included a screenshot showing more or less what I’m going for. In this example I’ve hardcoded the height of the Button
(so it looks pretty close), but this won’t work if the Text
‘s font size changes for any reason.
1