Is there a way to dynamically adjust font size based upon a user's position in a view?

38 Views Asked by At

I'm looking to create dynamically sized text for which the font size will shrink as the user scrolls down a List {} and then return to full size as the user scrolls back up to the top of the List {}. In the example below, the goal is that the "fraction" property would adjust down to close to zero as the user scrolled down the view, and then adjust back up to 1 as the scrolled back up. Thanks for any advice you have!

struct ContentView: View {

    @State var fraction: CGFloat = 1

    var body: some View {
        List {
            Text("Hello world!")
                .bold()
                .font(.system(size: 23*fraction))
                .frame(maxWidth: .infinity, alignment: .center)
                .listRowSeparator(.hidden)
            
            Section(header: Text("A list of things")) {
                ForEach((1...7), id: \.self) {
                    Text("\($0)")
                }
            }
            
            Section(header: Text("Another list")) {
                ForEach((1...7), id: \.self) {
                    Text("\($0)")
                }
            }
            
            Text("Some other content...")
            
        } // End of List
    } // End of body
}
0

There are 0 best solutions below