I have a design that I want to achieve in SwiftUI, and there are three examples shown below.
It's a VStack
that contains an Image
, and two Text
views.
regarding the idea with the placeholder, you can use native solutions such as .lineLimit(3, reservesSpace: true)
The part of the implementation should go like this which will guarantee your conditions I think.
import SwiftUI
struct ContentView: View {
@State var title = "Title text"
@State var subTitle = "Subtitle text"
var isSubtitleLarger: Bool {
subTitle.count > title.count
}
var body: some View {
VStack(alignment: .leading) {
Text(title)
.lineLimit(isSubtitleLarger ? 1 : 2)
Text(subTitle)
.lineLimit(isSubtitleLarger ? 2 : 1)
}
.frame(minHeight: 150) // Add minimum height to meet minimum size, so that the view doesn't shrink when texts are small
}
}
This is important to guarantee there is always 3 lines at max.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With