Skip to content

Instantly share code, notes, and snippets.

@christianselig
Last active September 5, 2022 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianselig/363c38e7c1e5d22bea8eeede5d3968c7 to your computer and use it in GitHub Desktop.
Save christianselig/363c38e7c1e5d22bea8eeede5d3968c7 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
let text1 = "Hello this is some very long text that goes on for quite awhile which can make it tricky to display"
let text2 = "I am brief"
var body: some View {
Color.black.opacity(0.2)
.frame(width: 180.0, height: 60.0)
.overlay(
VStack(alignment: .leading, spacing: 0.0) {
Text(text1)
.font(.system(size: 12.0, weight: .semibold))
Spacer(minLength: 0.0)
// .frame(minHeight: 0.0, maxHeight: 10.0)
Text("Buy My Ice Cream")
.font(.caption.italic())
}
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@marcpalmer
Copy link

Here you go:

import SwiftUI

struct ContentView: View {
    let text: String

    var body: some View {
        Color.black.opacity(0.2)
            .frame(width: 180.0, height: 60.0)
            .overlay(
                VStack(alignment: .leading, spacing: 0.0) {
                    Text(text)
                        .font(.system(size: 12.0, weight: .semibold))
                        .multilineTextAlignment(.leading)
                        .frame(maxWidth: .infinity, alignment: .leading)

                    Spacer()
                    .frame(minHeight: 0, maxHeight: 10.0)
                    .layoutPriority(-1)
                    
                    Text("Buy My Ice Cream")
                        .font(.caption.italic())
                        .multilineTextAlignment(.leading)
                        .frame(maxWidth: .infinity, alignment: .leading)
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
            )
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: 20) {
            ContentView(text: "Hello this is some very long text that goes on for quite awhile which can make it tricky to display")
            ContentView(text: "I am brief")
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment