I want to make a rectangle with text inside. the rectangle should be pinned to the right and left side of the display (or superview). It's height should be determined by the text.
I tried the following code:
struct DescriptionView: View {
var description =
"""
02.11.2021
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Illud urgueam, non intellegere eum quid sibi dicendum sit, cum dolorem summum malum esse dixerit. Omnis enim est natura diligens sui. Quamquam haec quidem praeposita recte et reiecta dicere licebit. Duo Reges: constructio interrete. Idem iste, inquam, de voluptate quid sentit? Ergo instituto veterum, quo etiam Stoici utuntur, hinc capiamus exordium. Tum ille: Tu autem cum ipse tantum librorum habeas, quos hic tandem requiris? Bona autem corporis huic sunt, quod posterius posui, similiora..
Bild © Lorem Lipsum
"""
var body: some View {
ZStack(alignment: .bottom) {
Color.red
.edgesIgnoringSafeArea(.all)
Text(description)
.foregroundColor(.white)
.font(.headline)
.background(
Rectangle()
)
.edgesIgnoringSafeArea(.horizontal)
}
}
}
As you can see, the text has some padding on the left and right side. How can I get rid of this? The rectangle should always be as wide as possible, while the text determines the height of the rectangle.
CodePudding user response:
Weird I tried this and it looks to work fine, which Xcode are you using?
Anyway I think you the modifier .edgesIgnoringSafeArea(.horizontal)
is not really needed there since you have applied it for .all
for the entire color which is the first image of the ZStack
CodePudding user response:
I don't know this is right for your needs, but this way can be answer.
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
VStack() {
Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to mak")
.font(.title)
.background(Color.blue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}