问题
I wanted to ask if following scenario would be possible and if yes how I could approach the problem. The following code produces the attached image:
HStack(alignment: .firstTextBaseline) {
ZStack {
RoundedRectangle(cornerRadius: 5)
.foregroundColor(Color(UIColor.secondarySystemFill))
Text("Test")
.clipShape(RoundedRectangle(cornerRadius: 5))
.foregroundColor(.secondary)
.layoutPriority(1)
.padding(.all, 5)
}
Text("This is a very long message with more words than one line can handle bla blabla blablabl")
}
Now in order to save more space I would like to have the next lines move to the left like so:
I have considered following approaches but none got me very far:
- Using Swift UIs
Text()+Text()instead of theHStack. This would not work as this method does not allow background colors (a .background()would converter theText()to some View and therefore I can't combine it with anotherText()). I could only change the.foregroundColor. - Maybe I could somehow write a
UIKitwrapper for aUILabelwithNSAttributedString. I don't like this method very much though, as changing background colors withNSAttributedStringisn't a very good solution either. - Use a
ZStackand maybe somehow throw a.clipShape()onto the longer right handText()so that it moves a little more to the right. This approach is also quite hacky since I don't quite know how to calculate the length of the first string so that I could move the second string.
Either I am missing something or this just isn't something that you could do in Swift UI or UIKit.
来源:https://stackoverflow.com/questions/62601163/how-to-wrap-text-around-some-view-in-a-hstack