本文介绍了在 ScrollView 中将文本对齐为前导 - SWIFTUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ScrollView:

    ScrollView {
        VStack (alignment: .leading) {
            Text("\(userData.username), \(userData.age)")
                .fontWeight(.semibold)
                .font(Font.system(size:30))
        }
    }

输出为:

我尝试翻转堆栈并在其中包含滚动视图,但我得到了相同的结果.

I tried flipping the Stack and having the scroll view inside, but I get the same result.

我的工作是把它放在一个 HStack 中并添加一个 Spacer()

The work around I have is to put it in a HStack and add a Spacer()

HStack {
         Text("\(userData.username), \(userData.age)")
             .fontWeight(.semibold)
             .font(Font.system(size:30))
             .padding([.leading])
         Spacer()
}

这可以解决问题,但我认为这不是理想的方法.这是唯一的方法吗?或者是否有更好的解决方案在滚动视图中垂直对齐文本?

This does the trick but I dont think this is the ideal way. Is this the only way? or is there a better solution to align text vertically in a scrollview?

推荐答案

默认情况下,堆栈容器对内容很紧,因此对齐没有效果.

By default stack container is tight to content, so alignment has no effect.

这是可能的解决方案

ScrollView {
    VStack (alignment: .leading) {
        Text("\(userData.username), \(userData.age)")
            .fontWeight(.semibold)
            .font(Font.system(size:30))
    }.frame(maxWidth: .infinity, alignment: .leading) // << make screen-wide !!
}

这篇关于在 ScrollView 中将文本对齐为前导 - SWIFTUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:05